/* {ok, DataSource} = lgeo_ogr:open("test/polygon.shp"), {ok, Layer} = lgeo_ogr:ds_get_layer(DataSource, 0), {ok, Feature} = lgeo_ogr:l_get_feature(Layer, 0), {ok, Geometry} = lgeo_ogr:f_get_geometry(Feature). */ static ERL_NIF_TERM f_get_geometry(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) { EnvFeature_t **feature; ERL_NIF_TERM eterm; if (argc != 1) { return enif_make_badarg(env); } if(!enif_get_resource(env, argv[0], OGR_F_RESOURCE, (void**)&feature)) { return enif_make_badarg(env); } OGRGeometryH geom = OGR_F_GetGeometryRef((**feature).obj); if(geom == NULL) { return enif_make_atom(env, "undefined"); } OGRGeometryH geom_clone = OGR_G_Clone(geom); EnvGeometry_t **geometry = \ enif_alloc_resource(OGR_G_RESOURCE, sizeof(EnvGeometry_t*)); *geometry = (EnvGeometry_t*) enif_alloc(sizeof(EnvGeometry_t)); (**geometry).env = NULL; (**geometry).obj = geom_clone; eterm = enif_make_resource(env, geometry); enif_release_resource(geometry); return enif_make_tuple2(env, enif_make_atom(env, "ok"), eterm); }
void setFromGeometry(OGRGeometryH geom) { if (geom) newRef(OGR_G_Clone(geom)); }
static CPLErr ProcessLayer( OGRLayerH hSrcLayer, int bSRSIsSet, GDALDatasetH hDstDS, std::vector<int> anBandList, const std::vector<double> &adfBurnValues, int b3D, int bInverse, const char *pszBurnAttribute, char **papszRasterizeOptions, GDALProgressFunc pfnProgress, void* pProgressData ) { /* -------------------------------------------------------------------- */ /* Checkout that SRS are the same. */ /* If -a_srs is specified, skip the test */ /* -------------------------------------------------------------------- */ OGRCoordinateTransformationH hCT = NULL; if (!bSRSIsSet) { OGRSpatialReferenceH hDstSRS = NULL; if( GDALGetProjectionRef( hDstDS ) != NULL ) { char *pszProjection; pszProjection = (char *) GDALGetProjectionRef( hDstDS ); hDstSRS = OSRNewSpatialReference(NULL); if( OSRImportFromWkt( hDstSRS, &pszProjection ) != OGRERR_NONE ) { OSRDestroySpatialReference(hDstSRS); hDstSRS = NULL; } } OGRSpatialReferenceH hSrcSRS = OGR_L_GetSpatialRef(hSrcLayer); if( hDstSRS != NULL && hSrcSRS != NULL ) { if( OSRIsSame(hSrcSRS, hDstSRS) == FALSE ) { hCT = OCTNewCoordinateTransformation(hSrcSRS, hDstSRS); if( hCT == NULL ) { CPLError(CE_Warning, CPLE_AppDefined, "The output raster dataset and the input vector layer do not have the same SRS.\n" "And reprojection of input data did not work. Results might be incorrect."); } } } else if( hDstSRS != NULL && hSrcSRS == NULL ) { CPLError(CE_Warning, CPLE_AppDefined, "The output raster dataset has a SRS, but the input vector layer SRS is unknown.\n" "Ensure input vector has the same SRS, otherwise results might be incorrect."); } else if( hDstSRS == NULL && hSrcSRS != NULL ) { CPLError(CE_Warning, CPLE_AppDefined, "The input vector layer has a SRS, but the output raster dataset SRS is unknown.\n" "Ensure output raster dataset has the same SRS, otherwise results might be incorrect."); } if( hDstSRS != NULL ) { OSRDestroySpatialReference(hDstSRS); } } /* -------------------------------------------------------------------- */ /* Get field index, and check. */ /* -------------------------------------------------------------------- */ int iBurnField = -1; if( pszBurnAttribute ) { iBurnField = OGR_FD_GetFieldIndex( OGR_L_GetLayerDefn( hSrcLayer ), pszBurnAttribute ); if( iBurnField == -1 ) { CPLError(CE_Failure, CPLE_AppDefined, "Failed to find field %s on layer %s, skipping.", pszBurnAttribute, OGR_FD_GetName( OGR_L_GetLayerDefn( hSrcLayer ) ) ); if( hCT != NULL ) OCTDestroyCoordinateTransformation(hCT); return CE_Failure; } } /* -------------------------------------------------------------------- */ /* Collect the geometries from this layer, and build list of */ /* burn values. */ /* -------------------------------------------------------------------- */ OGRFeatureH hFeat; std::vector<OGRGeometryH> ahGeometries; std::vector<double> adfFullBurnValues; OGR_L_ResetReading( hSrcLayer ); while( (hFeat = OGR_L_GetNextFeature( hSrcLayer )) != NULL ) { OGRGeometryH hGeom; if( OGR_F_GetGeometryRef( hFeat ) == NULL ) { OGR_F_Destroy( hFeat ); continue; } hGeom = OGR_G_Clone( OGR_F_GetGeometryRef( hFeat ) ); if( hCT != NULL ) { if( OGR_G_Transform(hGeom, hCT) != OGRERR_NONE ) { OGR_F_Destroy( hFeat ); OGR_G_DestroyGeometry(hGeom); continue; } } ahGeometries.push_back( hGeom ); for( unsigned int iBand = 0; iBand < anBandList.size(); iBand++ ) { if( adfBurnValues.size() > 0 ) adfFullBurnValues.push_back( adfBurnValues[MIN(iBand,adfBurnValues.size()-1)] ); else if( pszBurnAttribute ) { adfFullBurnValues.push_back( OGR_F_GetFieldAsDouble( hFeat, iBurnField ) ); } /* I have made the 3D option exclusive to other options since it can be used to modify the value from "-burn value" or "-a attribute_name" */ if( b3D ) { // TODO: get geometry "z" value /* Points and Lines will have their "z" values collected at the point and line levels respectively. However filled polygons (GDALdllImageFilledPolygon) can use some help by getting their "z" values here. */ adfFullBurnValues.push_back( 0.0 ); } } OGR_F_Destroy( hFeat ); } if( hCT != NULL ) OCTDestroyCoordinateTransformation(hCT); /* -------------------------------------------------------------------- */ /* If we are in inverse mode, we add one extra ring around the */ /* whole dataset to invert the concept of insideness and then */ /* merge everything into one geometry collection. */ /* -------------------------------------------------------------------- */ if( bInverse ) { if( ahGeometries.size() == 0 ) { for( unsigned int iBand = 0; iBand < anBandList.size(); iBand++ ) { if( adfBurnValues.size() > 0 ) adfFullBurnValues.push_back( adfBurnValues[MIN(iBand,adfBurnValues.size()-1)] ); else /* FIXME? Not sure what to do exactly in the else case, but we must insert a value */ adfFullBurnValues.push_back( 0.0 ); } } InvertGeometries( hDstDS, ahGeometries ); } /* -------------------------------------------------------------------- */ /* Perform the burn. */ /* -------------------------------------------------------------------- */ CPLErr eErr = GDALRasterizeGeometries( hDstDS, static_cast<int>(anBandList.size()), &(anBandList[0]), static_cast<int>(ahGeometries.size()), &(ahGeometries[0]), NULL, NULL, &(adfFullBurnValues[0]), papszRasterizeOptions, pfnProgress, pProgressData ); /* -------------------------------------------------------------------- */ /* Cleanup geometries. */ /* -------------------------------------------------------------------- */ int iGeom; for( iGeom = static_cast<int>(ahGeometries.size())-1; iGeom >= 0; iGeom-- ) OGR_G_DestroyGeometry( ahGeometries[iGeom] ); return eErr; }
std::string FetchTimeZone( double dfX, double dfY, const char *pszWkt ) { CPLDebug( "WINDNINJA", "Fetching timezone for %lf,%lf", dfX, dfY ); if( pszWkt != NULL ) { OGRSpatialReference oSourceSRS, oTargetSRS; OGRCoordinateTransformation *poCT; oSourceSRS.SetWellKnownGeogCS( "WGS84" ); oTargetSRS.importFromWkt( (char**)&pszWkt ); poCT = OGRCreateCoordinateTransformation( &oSourceSRS, &oTargetSRS ); if( poCT == NULL ) { CPLError( CE_Failure, CPLE_AppDefined, "OGR coordinate transformation failed" ); return std::string(); } if( !poCT->Transform( 1, &dfX, &dfY ) ) { CPLError( CE_Failure, CPLE_AppDefined, "OGR coordinate transformation failed" ); return std::string(); } OGRCoordinateTransformation::DestroyCT( poCT ); } OGRGeometryH hGeometry = OGR_G_CreateGeometry( wkbPoint ); OGR_G_SetPoint_2D( hGeometry, 0, dfX, dfY ); OGRDataSourceH hDS; OGRLayerH hLayer; OGRFeatureH hFeature; std::string oTzFile = FindDataPath( "tz_world.zip" ); oTzFile = "/vsizip/" + oTzFile + "/world/tz_world.shp"; hDS = OGROpen( oTzFile.c_str(), 0, NULL ); if( hDS == NULL ) { CPLError( CE_Failure, CPLE_AppDefined, "Failed to open datasource: %s", oTzFile.c_str() ); return std::string(); } hLayer = OGR_DS_GetLayer( hDS, 0 ); OGR_L_SetSpatialFilter( hLayer, hGeometry ); OGR_L_ResetReading( hLayer ); int nMaxTries = 5; int nTries = 0; OGRGeometryH hBufferGeometry; do { if( nTries == 0 ) { hBufferGeometry = OGR_G_Clone( hGeometry ); } else { hBufferGeometry = OGR_G_Buffer( hGeometry, 0.2 * nTries, 30 ); } OGR_L_SetSpatialFilter( hLayer, hBufferGeometry ); hFeature = OGR_L_GetNextFeature( hLayer ); OGR_G_DestroyGeometry( hBufferGeometry ); nTries++; } while( hFeature == NULL && nTries < nMaxTries ); std::string oTimeZone; if( hFeature == NULL ) { oTimeZone = std::string(); CPLError( CE_Failure, CPLE_AppDefined, "Failed to find timezone" ); } else { oTimeZone = std::string( OGR_F_GetFieldAsString( hFeature, 0 ) ); } OGR_F_Destroy( hFeature ); OGR_G_DestroyGeometry( hGeometry ); OGR_DS_Destroy( hDS ); return oTimeZone; }