SEXP ogrP4S(SEXP ogrsourcename, SEXP Layer) { #ifdef GDALV2 // GDALDriver *poDriver; GDALDataset *poDS; #else OGRSFDriver *poDriver; OGRDataSource *poDS; #endif OGRLayer *poLayer; OGRSpatialReference *hSRS = NULL; char *pszProj4 = NULL; SEXP ans; installErrorHandler(); #ifdef GDALV2 poDS=(GDALDataset*) GDALOpenEx(CHAR(STRING_ELT(ogrsourcename, 0)), GDAL_OF_VECTOR, NULL, NULL, NULL); #else poDS=OGRSFDriverRegistrar::Open(CHAR(STRING_ELT(ogrsourcename, 0)), FALSE, &poDriver); #endif uninstallErrorHandlerAndTriggerError(); if(poDS==NULL){ error("Cannot open file"); } installErrorHandler(); poLayer = poDS->GetLayerByName(CHAR(STRING_ELT(Layer, 0))); uninstallErrorHandlerAndTriggerError(); if(poLayer == NULL){ error("Cannot open layer"); } PROTECT(ans=NEW_CHARACTER(1)); installErrorHandler(); hSRS = poLayer->GetSpatialRef(); uninstallErrorHandlerAndTriggerError(); if (hSRS != NULL) { installErrorHandler(); hSRS->morphFromESRI(); if (hSRS->exportToProj4(&pszProj4) != OGRERR_NONE) { // SET_VECTOR_ELT(ans, 0, NA_STRING); SET_STRING_ELT(ans, 0, NA_STRING); } else { // SET_VECTOR_ELT(ans, 0, COPY_TO_USER_STRING(pszProj4)); SET_STRING_ELT(ans, 0, COPY_TO_USER_STRING(pszProj4)); } uninstallErrorHandlerAndTriggerError(); // } else SET_VECTOR_ELT(ans, 0, NA_STRING); } else SET_STRING_ELT(ans, 0, NA_STRING); installErrorHandler(); delete poDS; uninstallErrorHandlerAndTriggerError(); UNPROTECT(1); return(ans); }
int main() { GDALAllRegister(); //register all the format drivers cout << "GDAL All Registed!" << endl; GDALDataset *poDS; //Data source poDS = (GDALDataset*) GDALOpenEx("./beijing/road/Nbeijing_point.shp", GDAL_OF_VECTOR, NULL, NULL, NULL); if(poDS == NULL) { cout << "Open shp file failed!" << endl; exit(1); } cout << "Data source open success!" << endl; int layerNum = poDS->GetLayerCount(); //a dataset may have many layers cout << "Layer number:" << layerNum << endl; OGRLayer *poLayer; poLayer = poDS->GetLayerByName("Nbeijing_point"); //feature is a geometry and a set of attributes OGRFeature *poFeature; poLayer->ResetReading(); //Start at the beginning of the layer cout << "Feature number:" << poLayer->GetFeatureCount() << endl; stringstream ss; map<string, pair<double, double> > mip; map<string, pair<double, double> >::iterator imip; ofstream ofile("beijingNode"); string id; int crossFlag; string stmp, stmp2; vector<string> vs; vector<string>::iterator ivs; while((poFeature = poLayer->GetNextFeature()) != NULL) { //Defn contains the definition of all the fields OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn(); // for(iField = 0; iField < poFDefn->GetFieldCount(); iField++) // { // OGRFieldDefn * poFieldDefn = poFDefn->GetFieldDefn(iField); id = poFeature->GetFieldAsString(1); ofile << id; crossFlag = poFeature->GetFieldAsInteger(4); if(crossFlag == 0) { ofile << "\t" << crossFlag << "\t" << 0 << "\t" << 0 << "\t" << 0; } else if(crossFlag == 1) { ofile << "\t" << crossFlag << "\t" << 0 << "\t" << poFeature->GetFieldAsInteger(7) << "\t" << 0; } else if(crossFlag == 2) { stmp = poFeature->GetFieldAsString(6); vs = split(stmp, "|"); ofile << "\t" << crossFlag << "\t" << vs.size(); for(ivs = vs.begin(); ivs != vs.end(); ivs++) ofile << "\t" << *ivs; vs.clear(); ofile << "\t" << poFeature->GetFieldAsInteger(7) << "\t" << 0; } else if(crossFlag == 3) { stmp = poFeature->GetFieldAsString(6); vs = split(stmp, "|"); ofile << "\t" << crossFlag << "\t" << vs.size(); for(ivs = vs.begin(); ivs != vs.end(); ivs++) ofile << "\t" << *ivs; vs.clear(); ofile << "\t" << poFeature->GetFieldAsInteger(7); stmp = poFeature->GetFieldAsString(8); stmp2 = poFeature->GetFieldAsString(9); if(stmp2 != "") stmp += "|" + stmp2; vs = split(stmp, "|"); ofile << "\t" << vs.size(); for(ivs = vs.begin(); ivs != vs.end(); ivs++) ofile << "\t" << *ivs; vs.clear(); } ofile << "\t" << poFeature->GetFieldAsString(11); stmp = poFeature->GetFieldAsString(12); vs = split(stmp, "|"); ofile << "\t" << vs.size(); for(ivs = vs.begin(); ivs != vs.end(); ivs++) ofile << "\t" << *ivs; vs.clear(); /* if(poFieldDefn->GetType() == OFTInteger) cout << poFeature->GetFieldAsInteger(iField) << ", "; else if(poFieldDefn->GetType() == OFTInteger64) cout << poFeature->GetFieldAsInteger64(iField) << ", "; else if(poFieldDefn->GetType() == OFTReal) cout << setprecision(15) << poFeature->GetFieldAsDouble(iField) << ", "; else if(poFieldDefn->GetType() == OFTString) cout << poFeature->GetFieldAsString(iField) << ", "; else cout << poFeature->GetFieldAsString(iField) << ", ";*/ // } OGRGeometry *poGeometry; poGeometry = poFeature->GetGeometryRef(); // cout << "Geometry Type:" << poGeometry->getGeometryType() << endl; if(poGeometry != NULL && wkbFlatten(poGeometry->getGeometryType()) == wkbMultiPoint) { OGRMultiPoint *poMultiPoint = (OGRMultiPoint*)poGeometry; // cout << "Number in the MultiPoint:" << poMultiPoint->getNumGeometries() << endl; OGRGeometry *pG; pG = poMultiPoint->getGeometryRef(0); OGRPoint *pP = (OGRPoint*)pG; ofile << "\t" << pP->getY() << "\t" << pP->getX(); // pP->flattenTo2D(); // cout << setprecision(15) << pP->getY() << ", " << pP->getX() << endl; // mip[id] = make_pair(pP->getY(), pP->getX()); } else cout << "No Point Geometry" << endl; ofile << endl; OGRFeature::DestroyFeature(poFeature); } /* cout << "Writing nodes" << endl; for(imip = mip.begin(); imip != mip.end(); imip++) ofile << setprecision(15) << (*imip).first << "\t" << (*imip).second.first << "\t" << (*imip).second.second << endl;*/ GDALClose(poFeature); ofile.close(); return 0; }
static OGRGeometryCollection* LoadGeometry( const char* pszDS, const char* pszSQL, const char* pszLyr, const char* pszWhere ) { GDALDataset *poDS; OGRLayer *poLyr; OGRFeature *poFeat; OGRGeometryCollection *poGeom = NULL; poDS = (GDALDataset*) GDALOpen( pszDS, GA_ReadOnly ); if ( poDS == NULL ) return NULL; if ( pszSQL != NULL ) poLyr = poDS->ExecuteSQL( pszSQL, NULL, NULL ); else if ( pszLyr != NULL ) poLyr = poDS->GetLayerByName( pszLyr ); else poLyr = poDS->GetLayer(0); if ( poLyr == NULL ) { fprintf( stderr, "FAILURE: Failed to identify source layer from datasource.\n" ); GDALClose( (GDALDatasetH) poDS ); return NULL; } if ( pszWhere ) poLyr->SetAttributeFilter( pszWhere ); while ( (poFeat = poLyr->GetNextFeature()) != NULL ) { OGRGeometry* poSrcGeom = poFeat->GetGeometryRef(); if ( poSrcGeom ) { OGRwkbGeometryType eType = wkbFlatten( poSrcGeom->getGeometryType() ); if ( poGeom == NULL ) poGeom = new OGRMultiPolygon(); if ( eType == wkbPolygon ) poGeom->addGeometry( poSrcGeom ); else if ( eType == wkbMultiPolygon ) { int iGeom; int nGeomCount = ((OGRMultiPolygon *)poSrcGeom)->getNumGeometries(); for ( iGeom = 0; iGeom < nGeomCount; iGeom++ ) { poGeom->addGeometry( ((OGRMultiPolygon *)poSrcGeom)->getGeometryRef(iGeom) ); } } else { fprintf( stderr, "FAILURE: Geometry not of polygon type.\n" ); OGRGeometryFactory::destroyGeometry( poGeom ); OGRFeature::DestroyFeature( poFeat ); if ( pszSQL != NULL ) poDS->ReleaseResultSet( poLyr ); GDALClose( (GDALDatasetH) poDS ); return NULL; } } OGRFeature::DestroyFeature( poFeat ); } if( pszSQL != NULL ) poDS->ReleaseResultSet( poLyr ); GDALClose( (GDALDatasetH) poDS ); return poGeom; }
MAIN_START(nArgc, papszArgv) { // Check strict compilation and runtime library version as we use C++ API. if( !GDAL_CHECK_VERSION(papszArgv[0]) ) exit(1); EarlySetConfigOptions(nArgc, papszArgv); OGRRegisterAll(); /* -------------------------------------------------------------------- */ /* Processing command line arguments. */ /* -------------------------------------------------------------------- */ nArgc = OGRGeneralCmdLineProcessor(nArgc, &papszArgv, 0); if( nArgc < 1 ) exit(-nArgc); char *pszWHERE = nullptr; const char *pszDataSource = nullptr; char **papszLayers = nullptr; OGRGeometry *poSpatialFilter = nullptr; int nRepeatCount = 1; bool bAllLayers = false; char *pszSQLStatement = nullptr; const char *pszDialect = nullptr; int nRet = 0; const char* pszGeomField = nullptr; char **papszOpenOptions = nullptr; char **papszExtraMDDomains = nullptr; bool bListMDD = false; bool bShowMetadata = true; bool bFeatureCount = true; bool bExtent = true; bool bDatasetGetNextFeature = false; bool bReadOnly = false; bool bUpdate = false; const char* pszWKTFormat = "WKT2"; for( int iArg = 1; iArg < nArgc; iArg++ ) { if( EQUAL(papszArgv[iArg], "--utility_version") ) { printf("%s was compiled against GDAL %s and " "is running against GDAL %s\n", papszArgv[0], GDAL_RELEASE_NAME, GDALVersionInfo("RELEASE_NAME")); CSLDestroy(papszArgv); return 0; } else if( EQUAL(papszArgv[iArg], "--help") ) { Usage(); } else if( EQUAL(papszArgv[iArg], "-ro") ) { bReadOnly = true; } else if( EQUAL(papszArgv[iArg], "-update") ) { bUpdate = true; } else if( EQUAL(papszArgv[iArg], "-q") || EQUAL(papszArgv[iArg], "-quiet")) { bVerbose = false; } else if( EQUAL(papszArgv[iArg], "-qq") ) { /* Undocumented: mainly only useful for AFL testing */ bVerbose = false; bSuperQuiet = true; } else if( EQUAL(papszArgv[iArg], "-fid") ) { CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(1); nFetchFID = CPLAtoGIntBig(papszArgv[++iArg]); } else if( EQUAL(papszArgv[iArg], "-spat") ) { CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(4); OGRLinearRing oRing; oRing.addPoint(CPLAtof(papszArgv[iArg+1]), CPLAtof(papszArgv[iArg+2])); oRing.addPoint(CPLAtof(papszArgv[iArg+1]), CPLAtof(papszArgv[iArg+4])); oRing.addPoint(CPLAtof(papszArgv[iArg+3]), CPLAtof(papszArgv[iArg+4])); oRing.addPoint(CPLAtof(papszArgv[iArg+3]), CPLAtof(papszArgv[iArg+2])); oRing.addPoint(CPLAtof(papszArgv[iArg+1]), CPLAtof(papszArgv[iArg+2])); poSpatialFilter = new OGRPolygon(); static_cast<OGRPolygon *>(poSpatialFilter)->addRing(&oRing); iArg += 4; } else if( EQUAL(papszArgv[iArg], "-geomfield") ) { CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(1); pszGeomField = papszArgv[++iArg]; } else if( EQUAL(papszArgv[iArg], "-where") ) { CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(1); iArg++; CPLFree(pszWHERE); GByte* pabyRet = nullptr; if( papszArgv[iArg][0] == '@' && VSIIngestFile(nullptr, papszArgv[iArg] + 1, &pabyRet, nullptr, 1024*1024) ) { RemoveBOM(pabyRet); pszWHERE = reinterpret_cast<char *>(pabyRet); } else { pszWHERE = CPLStrdup(papszArgv[iArg]); } } else if( EQUAL(papszArgv[iArg], "-sql") ) { CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(1); iArg++; CPLFree(pszSQLStatement); GByte* pabyRet = nullptr; if( papszArgv[iArg][0] == '@' && VSIIngestFile(nullptr, papszArgv[iArg] + 1, &pabyRet, nullptr, 1024*1024) ) { RemoveBOM(pabyRet); pszSQLStatement = reinterpret_cast<char *>(pabyRet); RemoveSQLComments(pszSQLStatement); } else { pszSQLStatement = CPLStrdup(papszArgv[iArg]); } } else if( EQUAL(papszArgv[iArg], "-dialect") ) { CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(1); pszDialect = papszArgv[++iArg]; } else if( EQUAL(papszArgv[iArg], "-rc") ) { CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(1); nRepeatCount = atoi(papszArgv[++iArg]); } else if( EQUAL(papszArgv[iArg], "-al") ) { bAllLayers = true; } else if( EQUAL(papszArgv[iArg], "-so") || EQUAL(papszArgv[iArg], "-summary") ) { bSummaryOnly = true; } else if( STARTS_WITH_CI(papszArgv[iArg], "-fields=") ) { char* pszTemp = static_cast<char *>(CPLMalloc(32 + strlen(papszArgv[iArg]))); snprintf(pszTemp, 32 + strlen(papszArgv[iArg]), "DISPLAY_FIELDS=%s", papszArgv[iArg] + strlen("-fields=")); papszOptions = CSLAddString(papszOptions, pszTemp); CPLFree(pszTemp); } else if( STARTS_WITH_CI(papszArgv[iArg], "-geom=") ) { char* pszTemp = static_cast<char *>(CPLMalloc(32 + strlen(papszArgv[iArg]))); snprintf(pszTemp, 32 + strlen(papszArgv[iArg]), "DISPLAY_GEOMETRY=%s", papszArgv[iArg] + strlen("-geom=")); papszOptions = CSLAddString(papszOptions, pszTemp); CPLFree(pszTemp); } else if( EQUAL(papszArgv[iArg], "-oo") ) { CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(1); papszOpenOptions = CSLAddString(papszOpenOptions, papszArgv[++iArg]); } else if( EQUAL(papszArgv[iArg], "-nomd") ) { bShowMetadata = false; } else if( EQUAL(papszArgv[iArg], "-listmdd") ) { bListMDD = true; } else if( EQUAL(papszArgv[iArg], "-mdd") ) { CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(1); papszExtraMDDomains = CSLAddString(papszExtraMDDomains, papszArgv[++iArg]); } else if( EQUAL(papszArgv[iArg], "-nocount") ) { bFeatureCount = false; } else if( EQUAL(papszArgv[iArg], "-noextent") ) { bExtent = false; } else if( EQUAL(papszArgv[iArg], "-rl")) { bDatasetGetNextFeature = true; } else if( EQUAL(papszArgv[iArg], "-wkt_format") ) { CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(1); pszWKTFormat = papszArgv[++iArg]; } else if( papszArgv[iArg][0] == '-' ) { Usage(CPLSPrintf("Unknown option name '%s'", papszArgv[iArg])); } else if( pszDataSource == nullptr ) { pszDataSource = papszArgv[iArg]; } else { papszLayers = CSLAddString(papszLayers, papszArgv[iArg]); bAllLayers = false; } } if( pszDataSource == nullptr ) Usage("No datasource specified."); if( pszDialect != nullptr && pszWHERE != nullptr && pszSQLStatement == nullptr ) printf("Warning: -dialect is ignored with -where. Use -sql instead"); if( bDatasetGetNextFeature && pszSQLStatement ) { Usage("-rl is incompatible with -sql"); } #ifdef __AFL_HAVE_MANUAL_CONTROL while (__AFL_LOOP(1000)) { #endif /* -------------------------------------------------------------------- */ /* Open data source. */ /* -------------------------------------------------------------------- */ GDALDataset *poDS = static_cast<GDALDataset *>(GDALOpenEx( pszDataSource, ((bReadOnly || pszSQLStatement == nullptr) && !bUpdate ? GDAL_OF_READONLY : GDAL_OF_UPDATE) | GDAL_OF_VECTOR, nullptr, papszOpenOptions, nullptr)); if( poDS == nullptr && !bReadOnly && !bUpdate && pszSQLStatement == nullptr ) { // In some cases (empty geopackage for example), opening in read-only // mode fails, so retry in update mode if( GDALIdentifyDriverEx(pszDataSource, GDAL_OF_VECTOR, nullptr, nullptr) ) { poDS = static_cast<GDALDataset *>(GDALOpenEx( pszDataSource, GDAL_OF_UPDATE | GDAL_OF_VECTOR, nullptr, papszOpenOptions, nullptr)); } } if( poDS == nullptr && !bReadOnly && !bUpdate && pszSQLStatement != nullptr ) { poDS = static_cast<GDALDataset *>(GDALOpenEx( pszDataSource, GDAL_OF_READONLY | GDAL_OF_VECTOR, nullptr, papszOpenOptions, nullptr)); if( poDS != nullptr && bVerbose ) { printf("Had to open data source read-only.\n"); #ifdef __AFL_HAVE_MANUAL_CONTROL bReadOnly = true; #endif } } GDALDriver *poDriver = nullptr; if( poDS != nullptr ) poDriver = poDS->GetDriver(); /* -------------------------------------------------------------------- */ /* Report failure */ /* -------------------------------------------------------------------- */ if( poDS == nullptr ) { printf("FAILURE:\n" "Unable to open datasource `%s' with the following drivers.\n", pszDataSource); #ifdef __AFL_HAVE_MANUAL_CONTROL continue; #else OGRSFDriverRegistrar *poR = OGRSFDriverRegistrar::GetRegistrar(); for( int iDriver = 0; iDriver < poR->GetDriverCount(); iDriver++ ) { printf(" -> %s\n", poR->GetDriver(iDriver)->GetDescription()); } nRet = 1; goto end; #endif } CPLAssert(poDriver != nullptr); /* -------------------------------------------------------------------- */ /* Some information messages. */ /* -------------------------------------------------------------------- */ if( bVerbose ) printf("INFO: Open of `%s'\n" " using driver `%s' successful.\n", pszDataSource, poDriver->GetDescription()); if( bVerbose && !EQUAL(pszDataSource,poDS->GetDescription()) ) { printf("INFO: Internal data source name `%s'\n" " different from user name `%s'.\n", poDS->GetDescription(), pszDataSource); } GDALInfoReportMetadata(static_cast<GDALMajorObjectH>(poDS), bListMDD, bShowMetadata, papszExtraMDDomains); if( bDatasetGetNextFeature ) { nRepeatCount = 0; // skip layer reporting. /* -------------------------------------------------------------------- */ /* Set filters if provided. */ /* -------------------------------------------------------------------- */ if( pszWHERE != nullptr || poSpatialFilter != nullptr ) { for( int iLayer = 0; iLayer < poDS->GetLayerCount(); iLayer++ ) { OGRLayer *poLayer = poDS->GetLayer(iLayer); if( poLayer == nullptr ) { printf("FAILURE: Couldn't fetch advertised layer %d!\n", iLayer); exit(1); } if( pszWHERE != nullptr ) { if( poLayer->SetAttributeFilter(pszWHERE) != OGRERR_NONE ) { printf("WARNING: SetAttributeFilter(%s) " "failed on layer %s.\n", pszWHERE, poLayer->GetName()); } } if( poSpatialFilter != nullptr ) { if( pszGeomField != nullptr ) { OGRFeatureDefn *poDefn = poLayer->GetLayerDefn(); const int iGeomField = poDefn->GetGeomFieldIndex(pszGeomField); if( iGeomField >= 0 ) poLayer->SetSpatialFilter(iGeomField, poSpatialFilter); else printf("WARNING: Cannot find geometry field %s.\n", pszGeomField); } else { poLayer->SetSpatialFilter(poSpatialFilter); } } } } std::set<OGRLayer*> oSetLayers; while( true ) { OGRLayer* poLayer = nullptr; OGRFeature* poFeature = poDS->GetNextFeature(&poLayer, nullptr, nullptr, nullptr); if( poFeature == nullptr ) break; if( papszLayers == nullptr || poLayer == nullptr || CSLFindString(papszLayers, poLayer->GetName()) >= 0 ) { if( bVerbose && poLayer != nullptr && oSetLayers.find(poLayer) == oSetLayers.end() ) { oSetLayers.insert(poLayer); const bool bSummaryOnlyBackup = bSummaryOnly; bSummaryOnly = true; ReportOnLayer(poLayer, nullptr, nullptr, nullptr, bListMDD, bShowMetadata, papszExtraMDDomains, bFeatureCount, bExtent, pszWKTFormat); bSummaryOnly = bSummaryOnlyBackup; } if( !bSuperQuiet && !bSummaryOnly ) poFeature->DumpReadable(nullptr, papszOptions); } OGRFeature::DestroyFeature(poFeature); } } /* -------------------------------------------------------------------- */ /* Special case for -sql clause. No source layers required. */ /* -------------------------------------------------------------------- */ else if( pszSQLStatement != nullptr ) { nRepeatCount = 0; // skip layer reporting. if( CSLCount(papszLayers) > 0 ) printf("layer names ignored in combination with -sql.\n"); OGRLayer *poResultSet = poDS->ExecuteSQL( pszSQLStatement, pszGeomField == nullptr ? poSpatialFilter : nullptr, pszDialect); if( poResultSet != nullptr ) { if( pszWHERE != nullptr ) { if( poResultSet->SetAttributeFilter(pszWHERE) != OGRERR_NONE ) { printf("FAILURE: SetAttributeFilter(%s) failed.\n", pszWHERE); exit(1); } } if( pszGeomField != nullptr ) ReportOnLayer(poResultSet, nullptr, pszGeomField, poSpatialFilter, bListMDD, bShowMetadata, papszExtraMDDomains, bFeatureCount, bExtent, pszWKTFormat); else ReportOnLayer(poResultSet, nullptr, nullptr, nullptr, bListMDD, bShowMetadata, papszExtraMDDomains, bFeatureCount, bExtent, pszWKTFormat); poDS->ReleaseResultSet(poResultSet); } } // coverity[tainted_data] for( int iRepeat = 0; iRepeat < nRepeatCount; iRepeat++ ) { if( papszLayers == nullptr || *papszLayers == nullptr ) { if( iRepeat == 0 ) CPLDebug("OGR", "GetLayerCount() = %d\n", poDS->GetLayerCount()); /* -------------------------------------------------------------------- */ /* Process each data source layer. */ /* -------------------------------------------------------------------- */ for( int iLayer = 0; iLayer < poDS->GetLayerCount(); iLayer++ ) { OGRLayer *poLayer = poDS->GetLayer(iLayer); if( poLayer == nullptr ) { printf("FAILURE: Couldn't fetch advertised layer %d!\n", iLayer); exit(1); } if( !bAllLayers ) { printf("%d: %s", iLayer + 1, poLayer->GetName()); const int nGeomFieldCount = poLayer->GetLayerDefn()->GetGeomFieldCount(); if( nGeomFieldCount > 1 ) { printf(" ("); for( int iGeom = 0; iGeom < nGeomFieldCount; iGeom++ ) { if( iGeom > 0 ) printf(", "); OGRGeomFieldDefn* poGFldDefn = poLayer->GetLayerDefn()-> GetGeomFieldDefn(iGeom); printf( "%s", OGRGeometryTypeToName( poGFldDefn->GetType())); } printf(")"); } else if( poLayer->GetGeomType() != wkbUnknown ) printf(" (%s)", OGRGeometryTypeToName( poLayer->GetGeomType())); printf("\n"); } else { if( iRepeat != 0 ) poLayer->ResetReading(); ReportOnLayer(poLayer, pszWHERE, pszGeomField, poSpatialFilter, bListMDD, bShowMetadata, papszExtraMDDomains, bFeatureCount, bExtent, pszWKTFormat); } } } else { /* -------------------------------------------------------------------- */ /* Process specified data source layers. */ /* -------------------------------------------------------------------- */ for( char** papszIter = papszLayers; *papszIter != nullptr; ++papszIter ) { OGRLayer *poLayer = poDS->GetLayerByName(*papszIter); if( poLayer == nullptr ) { printf("FAILURE: Couldn't fetch requested layer %s!\n", *papszIter); exit(1); } if( iRepeat != 0 ) poLayer->ResetReading(); ReportOnLayer(poLayer, pszWHERE, pszGeomField, poSpatialFilter, bListMDD, bShowMetadata, papszExtraMDDomains, bFeatureCount, bExtent, pszWKTFormat); } } } /* -------------------------------------------------------------------- */ /* Close down. */ /* -------------------------------------------------------------------- */ GDALClose(poDS); #ifdef __AFL_HAVE_MANUAL_CONTROL } #else end: #endif CSLDestroy(papszArgv); CSLDestroy(papszLayers); CSLDestroy(papszOptions); CSLDestroy(papszOpenOptions); CSLDestroy(papszExtraMDDomains); if( poSpatialFilter ) OGRGeometryFactory::destroyGeometry(poSpatialFilter); CPLFree(pszSQLStatement); CPLFree(pszWHERE); OGRCleanupAll(); return nRet; }
SEXP ogrCheckExists (SEXP ogrSource, SEXP Layer) { OGRLayer *poLayer; #ifdef GDALV2 GDALDataset *poDS; GDALDriver *poDriver; #else OGRDataSource *poDS; OGRSFDriver *poDriver; #endif SEXP ans, drv; int pc=0; PROTECT(ans=NEW_LOGICAL(1)); pc++; installErrorHandler(); #ifdef GDALV2 poDS=(GDALDataset*) GDALOpenEx(CHAR(STRING_ELT(ogrSource, 0)), GDAL_OF_VECTOR, NULL, NULL, NULL); if (poDS != NULL) poDriver = poDS->GetDriver(); #else poDS=OGRSFDriverRegistrar::Open(CHAR(STRING_ELT(ogrSource, 0)), FALSE, &poDriver); #endif uninstallErrorHandlerAndTriggerError(); if (poDS==NULL) { // installErrorHandler(); // OGRDataSource::DestroyDataSource( poDS ); // uninstallErrorHandlerAndTriggerError(); // delete poDS; LOGICAL_POINTER(ans)[0] = FALSE; UNPROTECT(pc); return(ans); } installErrorHandler(); poLayer = poDS->GetLayerByName(CHAR(STRING_ELT(Layer, 0))); uninstallErrorHandlerAndTriggerError(); if (poLayer == NULL) { installErrorHandler(); #ifdef GDALV2 GDALClose( poDS ); #else OGRDataSource::DestroyDataSource( poDS ); #endif uninstallErrorHandlerAndTriggerError(); // delete poDS; LOGICAL_POINTER(ans)[0] = FALSE; UNPROTECT(pc); return(ans); } LOGICAL_POINTER(ans)[0] = TRUE; PROTECT(drv=allocVector(STRSXP,1)); pc++; installErrorHandler(); #ifdef GDALV2 SET_STRING_ELT(drv, 0, mkChar(poDriver->GetDescription())); #else SET_STRING_ELT(drv, 0, mkChar(poDriver->GetName())); #endif uninstallErrorHandlerAndTriggerError(); setAttrib(ans, install("driver"), drv); installErrorHandler(); #ifdef GDALV2 GDALClose( poDS ); #else OGRDataSource::DestroyDataSource( poDS ); #endif uninstallErrorHandlerAndTriggerError(); // delete poDS; UNPROTECT(pc); return(ans); }
// extern "C" { SEXP ogrDataFrame(SEXP ogrSource, SEXP Layer, SEXP FIDs, SEXP iFields) { // query an OGR data source and return a list SEXP ans; SEXP nListFields, ListFields=R_NilValue, int64; OGRLayer *poLayer; #ifdef GDALV2 GDALDataset *poDS; #else OGRDataSource *poDS; OGRSFDriver *poDriver; #endif int iField, nflds=length(iFields), j=0, k; int pc=0; // open the data source layer or error installErrorHandler(); #ifdef GDALV2 poDS=(GDALDataset*) GDALOpenEx(CHAR(STRING_ELT(ogrSource, 0)), GDAL_OF_VECTOR, NULL, NULL, NULL); #else poDS=OGRSFDriverRegistrar::Open(CHAR(STRING_ELT(ogrSource,0)), FALSE, &poDriver); #endif uninstallErrorHandlerAndTriggerError(); if(poDS==NULL) { error("Cannot open file"); } installErrorHandler(); poLayer = poDS->GetLayerByName(CHAR(STRING_ELT(Layer, 0))); uninstallErrorHandlerAndTriggerError(); if(poLayer == NULL) { error("Cannot open layer"); } int64 = getAttrib(iFields, mkString("int64")); nListFields = getAttrib(iFields, mkString("nListFields")); SEXP ENC_attr = getAttrib(iFields, mkString("ENCODING_DEBUG")); int ENC_DEBUG = LOGICAL_POINTER(ENC_attr)[0]; // reserve a list for the result if (INTEGER_POINTER(nListFields)[0] == 0) { PROTECT(ans=allocVector(VECSXP,length(iFields))); pc++; } else { nflds = INTEGER_POINTER(getAttrib(iFields, mkString("nflds")))[0]; PROTECT(ans=allocVector(VECSXP,nflds)); pc++; ListFields = getAttrib(iFields, mkString("ListFields")); } // now set each element of the list installErrorHandler(); if (INTEGER_POINTER(nListFields)[0] == 0) { for(iField=0; iField<length(iFields); iField++) { SET_VECTOR_ELT(ans,iField,ogrReadColumn(poLayer, FIDs, INTEGER(iFields)[iField], INTEGER(int64)[0], ENC_DEBUG)); } } else { j=0; for(iField=0; iField<length(iFields); iField++) { if (INTEGER_POINTER(ListFields)[iField] == 0) { SET_VECTOR_ELT(ans, j, ogrReadColumn(poLayer, FIDs, INTEGER(iFields)[iField], INTEGER(int64)[0], ENC_DEBUG)); j++; } else { for (k=0; k < INTEGER_POINTER(ListFields)[iField]; k++) { SET_VECTOR_ELT(ans, j, ogrReadListColumn(poLayer, FIDs, INTEGER(iFields)[iField], k, INTEGER(int64)[0])); j++; } } } } uninstallErrorHandlerAndTriggerError(); // clean up and return installErrorHandler(); #ifdef GDALV2 GDALClose( poDS ); #else OGRDataSource::DestroyDataSource( poDS ); #endif // delete poDS; uninstallErrorHandlerAndTriggerError(); UNPROTECT(pc); return(ans); }
// extern "C" { SEXP ogrInfo(SEXP ogrsourcename, SEXP Layer) { // return FIDs, nFields, fieldInfo SEXP ans, vec1, vec2, vec3,/*mat,*/drv, dvec; SEXP itemlist, itemnames, itemwidth, itemtype, itemTypeNames; SEXP itemlistmaxcount; #ifdef GDALV2 SEXP dFIDs; #endif /*SEXP geotype;*/ int nFIDs, nFields, iField, *nCount, pc=0; #ifdef GDALV2 GDALDriver *poDriver; GDALDataset *poDS; #else OGRDataSource *poDS; OGRSFDriver *poDriver; #endif OGRLayer *poLayer; OGRFeature *poFeature; OGRFeatureDefn *poDefn; /* OGRGeometry *poGeom;*/ installErrorHandler(); #ifdef GDALV2 poDS=(GDALDataset*) GDALOpenEx(CHAR(STRING_ELT(ogrsourcename, 0)), GDAL_OF_VECTOR, NULL, NULL, NULL); if(poDS==NULL) { uninstallErrorHandlerAndTriggerError(); error("Cannot open data source"); } poDriver = poDS->GetDriver(); #else poDS=OGRSFDriverRegistrar::Open(CHAR(STRING_ELT(ogrsourcename, 0)), FALSE, &poDriver); #endif uninstallErrorHandlerAndTriggerError(); if(poDS==NULL) { installErrorHandler(); #ifdef GDALV2 GDALClose( poDS ); #else OGRDataSource::DestroyDataSource( poDS ); #endif uninstallErrorHandlerAndTriggerError(); // delete poDS; error("Cannot open file"); } installErrorHandler(); poLayer = poDS->GetLayerByName(CHAR(STRING_ELT(Layer, 0))); uninstallErrorHandlerAndTriggerError(); if(poLayer == NULL) { installErrorHandler(); #ifdef GDALV2 GDALClose( poDS ); #else OGRDataSource::DestroyDataSource( poDS ); #endif uninstallErrorHandlerAndTriggerError(); // delete poDS; error("Cannot open layer"); } // allocate a list for return values PROTECT(ans=allocVector(VECSXP,6)); pc++; PROTECT(drv=allocVector(STRSXP,1)); pc++; installErrorHandler(); #ifdef GDALV2 SET_STRING_ELT(drv, 0, mkChar(poDriver->GetDescription())); #else SET_STRING_ELT(drv, 0, mkChar(poDriver->GetName())); #endif uninstallErrorHandlerAndTriggerError(); SET_VECTOR_ELT(ans,3,drv); PROTECT(vec1=allocVector(INTSXP,1)); pc++; installErrorHandler(); #ifdef GDALV2 GIntBig nFIDs64 = poLayer->GetFeatureCount(); nFIDs = (nFIDs64 > INT_MAX) ? INT_MAX : (nFIDs64 < INT_MIN) ? INT_MIN : (int) nFIDs64; if ((GIntBig) nFIDs != nFIDs64) { warning("ogrInfo: feature count overflow"); INTEGER(vec1)[0]=NA_INTEGER; PROTECT(dFIDs=NEW_NUMERIC(1)); pc++; NUMERIC_POINTER(dFIDs)[0] = (double) nFIDs64; setAttrib(vec1, install("dFIDs"), dFIDs); } else { // store number of FIDs INTEGER(vec1)[0]=nFIDs; } #else nFIDs = poLayer->GetFeatureCount(); // store number of FIDs INTEGER(vec1)[0]=nFIDs; #endif uninstallErrorHandlerAndTriggerError(); if (nFIDs == -1) { int i=0; installErrorHandler(); while( ((poFeature = poLayer->GetNextFeature()) != NULL) && i <= INT_MAX) { i++; OGRFeature::DestroyFeature( poFeature ); // delete poFeature; } uninstallErrorHandlerAndTriggerError(); if (i == INT_MAX) { error("ogrInfo: undeclared feature count overflow"); } else { nFIDs = i; warning("ogrInfo: feature count not given; %d counted", nFIDs); } installErrorHandler(); poLayer->ResetReading(); uninstallErrorHandlerAndTriggerError(); INTEGER(vec1)[0]=nFIDs; } SET_VECTOR_ELT(ans,0,vec1); // store other stuff.... installErrorHandler(); poDefn = poLayer->GetLayerDefn(); nFields = poDefn->GetFieldCount(); uninstallErrorHandlerAndTriggerError(); // store number of fields PROTECT(vec2=allocVector(INTSXP,1)); pc++; INTEGER(vec2)[0]=nFields; SET_VECTOR_ELT(ans,1,vec2); installErrorHandler(); OGREnvelope oExt; if (poLayer->GetExtent(&oExt, TRUE) == OGRERR_NONE) { PROTECT(dvec=allocVector(REALSXP,4)); pc++; REAL(dvec)[0] = oExt.MinX; REAL(dvec)[1] = oExt.MinY; REAL(dvec)[2] = oExt.MaxX; REAL(dvec)[3] = oExt.MaxY; SET_VECTOR_ELT(ans,4,dvec); } uninstallErrorHandlerAndTriggerError(); PROTECT(itemnames=allocVector(STRSXP,nFields)); pc++; PROTECT(itemtype=allocVector(INTSXP,nFields)); pc++; PROTECT(itemwidth=allocVector(INTSXP,nFields)); pc++; // try List types PROTECT(itemlistmaxcount=allocVector(INTSXP,nFields)); pc++; PROTECT(itemTypeNames=allocVector(STRSXP,nFields)); pc++; int listFieldCount=0; installErrorHandler(); for(iField=0; iField<nFields; iField++) { OGRFieldDefn *poField = poDefn->GetFieldDefn(iField); SET_STRING_ELT(itemnames,iField,mkChar(poField->GetNameRef())); INTEGER(itemtype)[iField]=poField->GetType(); if (INTEGER(itemtype)[iField] == OFTIntegerList || INTEGER(itemtype)[iField] == OFTRealList || INTEGER(itemtype)[iField] == OFTStringList) listFieldCount++; INTEGER(itemwidth)[iField]=poField->GetWidth(); SET_STRING_ELT(itemTypeNames,iField,mkChar(poField->GetFieldTypeName( poField->GetType()))); INTEGER(itemlistmaxcount)[iField] = 0; } uninstallErrorHandlerAndTriggerError(); PROTECT(vec3=allocVector(INTSXP,1)); pc++; INTEGER(vec3)[0]=listFieldCount; SET_VECTOR_ELT(ans,5,vec3); PROTECT(itemlist=allocVector(VECSXP,5)); pc++; SET_VECTOR_ELT(itemlist,0,itemnames); SET_VECTOR_ELT(itemlist,1,itemtype); SET_VECTOR_ELT(itemlist,2,itemwidth); SET_VECTOR_ELT(itemlist,3,itemTypeNames); // try List types if (listFieldCount > 0) { poLayer->ResetReading(); OGRFeature* poFeature; nCount = (int *) R_alloc((size_t) nFields, sizeof(int)); for (iField=0; iField<nFields; iField++) nCount[iField] = 0; installErrorHandler(); OGRField* psField; while( (poFeature = poLayer->GetNextFeature()) != NULL ) { for(iField=0; iField<nFields; iField++) { psField = poFeature->GetRawFieldRef(iField); if (INTEGER(itemtype)[iField] == OFTIntegerList) { nCount[iField] = psField->IntegerList.nCount; if (nCount[iField] > INTEGER(itemlistmaxcount)[iField]) INTEGER(itemlistmaxcount)[iField] = nCount[iField]; } else if (INTEGER(itemtype)[iField] == OFTRealList) { nCount[iField] = psField->RealList.nCount; if (nCount[iField] > INTEGER(itemlistmaxcount)[iField]) INTEGER(itemlistmaxcount)[iField] = nCount[iField]; } else if (INTEGER(itemtype)[iField] == OFTStringList) { nCount[iField] = psField->StringList.nCount; if (nCount[iField] > INTEGER(itemlistmaxcount)[iField]) INTEGER(itemlistmaxcount)[iField] = nCount[iField]; } } OGRFeature::DestroyFeature( poFeature ); // delete poFeature; } uninstallErrorHandlerAndTriggerError(); } SET_VECTOR_ELT(itemlist,4,itemlistmaxcount); SET_VECTOR_ELT(ans,2,itemlist); UNPROTECT(pc); installErrorHandler(); #ifdef GDALV2 GDALClose( poDS ); #else OGRDataSource::DestroyDataSource( poDS ); #endif uninstallErrorHandlerAndTriggerError(); // delete poDS; return(ans); }
//extern "C" { SEXP ogrFIDs(SEXP filename, SEXP layer) { SEXP fids, nf, ii; int /*layerNum,*/i; int nFeatures, pc=0; OGRLayer *poLayer; OGRFeature *poFeature; #ifdef GDALV2 GDALDataset *poDS; #else OGRDataSource *poDS; OGRSFDriver *poDriver; #endif installErrorHandler(); #ifdef GDALV2 poDS=(GDALDataset*) GDALOpenEx(CHAR(STRING_ELT(filename, 0)), GDAL_OF_VECTOR, NULL, NULL, NULL); #else poDS=OGRSFDriverRegistrar::Open(CHAR(STRING_ELT(filename, 0)), FALSE, &poDriver); #endif uninstallErrorHandlerAndTriggerError(); if(poDS==NULL) { error("Cannot open file"); } installErrorHandler(); poLayer = poDS->GetLayerByName(CHAR(STRING_ELT(layer, 0))); uninstallErrorHandlerAndTriggerError(); if(poLayer == NULL) { error("Cannot open layer"); } installErrorHandler(); #ifdef GDALV2 GIntBig nFIDs64 = poLayer->GetFeatureCount(); nFeatures = (nFIDs64 > INT_MAX) ? INT_MAX : (nFIDs64 < INT_MIN) ? INT_MIN : (int) nFIDs64; if ((GIntBig) nFeatures != nFIDs64) { uninstallErrorHandlerAndTriggerError(); error("ogrFIDs: feature count overflow"); } #else nFeatures=poLayer->GetFeatureCount(); #endif uninstallErrorHandlerAndTriggerError(); if (nFeatures == -1) { i=0; installErrorHandler(); while( ((poFeature = poLayer->GetNextFeature()) != NULL) && i <= INT_MAX) { i++; OGRFeature::DestroyFeature( poFeature ); // delete poFeature; } uninstallErrorHandlerAndTriggerError(); installErrorHandler(); poLayer->ResetReading(); uninstallErrorHandlerAndTriggerError(); if (i == INT_MAX) { error("ogrFIDs: feature count overflow"); } else { nFeatures = i; } } PROTECT(fids=allocVector(INTSXP,nFeatures)); pc++; PROTECT(nf = NEW_INTEGER(1)); pc++; INTEGER_POINTER(nf)[0] = nFeatures; PROTECT(ii = NEW_INTEGER(1)); pc++; installErrorHandler(); poLayer->ResetReading(); uninstallErrorHandlerAndTriggerError(); i=0; installErrorHandler(); while( (poFeature = poLayer->GetNextFeature()) != NULL ) { INTEGER(fids)[i]= (int) poFeature->GetFID(); i++; OGRFeature::DestroyFeature( poFeature ); // delete poFeature; } uninstallErrorHandlerAndTriggerError(); INTEGER_POINTER(ii)[0] = i; setAttrib(fids, install("nf"), nf); setAttrib(fids, install("i"), ii); installErrorHandler(); #ifdef GDALV2 GDALClose( poDS ); #else OGRDataSource::DestroyDataSource( poDS ); #endif uninstallErrorHandlerAndTriggerError(); // delete poDS; UNPROTECT(pc); return(fids); }