explicit ProblemReporterOGR(OGRDataSource* data_source) : m_data_source(data_source) { OGRSpatialReference sparef; sparef.SetWellKnownGeogCS("WGS84"); m_layer_perror = m_data_source->CreateLayer("perrors", &sparef, wkbPoint, nullptr); if (!m_layer_perror) { std::runtime_error("Layer creation failed for layer 'perrors'"); } OGRFieldDefn layer_perror_field_id1("id1", OFTReal); layer_perror_field_id1.SetWidth(10); if (m_layer_perror->CreateField(&layer_perror_field_id1) != OGRERR_NONE) { std::runtime_error("Creating field 'id1' failed for layer 'perrors'"); } OGRFieldDefn layer_perror_field_id2("id2", OFTReal); layer_perror_field_id2.SetWidth(10); if (m_layer_perror->CreateField(&layer_perror_field_id2) != OGRERR_NONE) { std::runtime_error("Creating field 'id2' failed for layer 'perrors'"); } OGRFieldDefn layer_perror_field_problem_type("problem_type", OFTString); layer_perror_field_problem_type.SetWidth(30); if (m_layer_perror->CreateField(&layer_perror_field_problem_type) != OGRERR_NONE) { std::runtime_error("Creating field 'problem_type' failed for layer 'perrors'"); } /**************/ m_layer_lerror = m_data_source->CreateLayer("lerrors", &sparef, wkbLineString, nullptr); if (!m_layer_lerror) { std::runtime_error("Layer creation failed for layer 'lerrors'"); } OGRFieldDefn layer_lerror_field_id1("id1", OFTReal); layer_lerror_field_id1.SetWidth(10); if (m_layer_lerror->CreateField(&layer_lerror_field_id1) != OGRERR_NONE) { std::runtime_error("Creating field 'id1' failed for layer 'lerrors'"); } OGRFieldDefn layer_lerror_field_id2("id2", OFTReal); layer_lerror_field_id2.SetWidth(10); if (m_layer_lerror->CreateField(&layer_lerror_field_id2) != OGRERR_NONE) { std::runtime_error("Creating field 'id2' failed for layer 'lerrors'"); } OGRFieldDefn layer_lerror_field_problem_type("problem_type", OFTString); layer_lerror_field_problem_type.SetWidth(30); if (m_layer_lerror->CreateField(&layer_lerror_field_problem_type) != OGRERR_NONE) { std::runtime_error("Creating field 'problem_type' failed for layer 'lerrors'"); } }
OGRDataSourceH OGR_Dr_CreateDataSource( OGRSFDriverH hDriver, const char *pszName, char ** papszOptions ) { VALIDATE_POINTER1( hDriver, "OGR_Dr_CreateDataSource", NULL ); OGRSFDriver* poDriver = (OGRSFDriver *) hDriver; CPLAssert( NULL != poDriver ); OGRDataSource* poDS = NULL; poDS = poDriver->CreateDataSource( pszName, papszOptions ); /* This fix is explained in Ticket #1223 */ if( NULL != poDS ) { poDS->SetDriver( poDriver ); CPLAssert( NULL != poDS->GetDriver() ); } else { CPLDebug( "OGR", "CreateDataSource operation failed. NULL pointer returned." ); } return (OGRDataSourceH) poDS; }
OGRDataSource *OGRSFDriver::CopyDataSource( OGRDataSource *poSrcDS, const char *pszNewName, char **papszOptions ) { if( !TestCapability( ODrCCreateDataSource ) ) { CPLError( CE_Failure, CPLE_NotSupported, "%s driver does not support data source creation.", GetName() ); return NULL; } OGRDataSource *poODS; poODS = CreateDataSource( pszNewName, papszOptions ); if( poODS == NULL ) return NULL; /* -------------------------------------------------------------------- */ /* Process each data source layer. */ /* -------------------------------------------------------------------- */ for( int iLayer = 0; iLayer < poSrcDS->GetLayerCount(); iLayer++ ) { OGRLayer *poLayer = poSrcDS->GetLayer(iLayer); if( poLayer == NULL ) continue; poODS->CopyLayer( poLayer, poLayer->GetLayerDefn()->GetName(), papszOptions ); } return poODS; }
virtual ReadResult readFile(const std::string& fileName, const osgDB::ReaderWriter::Options* options) const { if (OGRSFDriverRegistrar::GetRegistrar()->GetDriverCount() == 0) OGRRegisterAll(); // Try to open data source OGRDataSource* file = OGRSFDriverRegistrar::Open(fileName.c_str()); if (!file) return 0; bool useRandomColorByFeature = false; bool addGroupPerFeature = false; if (options) { if (options->getOptionString().find("UseRandomColorByFeature") != std::string::npos) useRandomColorByFeature = true; if (options->getOptionString().find("useRandomColorByFeature") != std::string::npos) useRandomColorByFeature = true; if (options->getOptionString().find("addGroupPerFeature") != std::string::npos) addGroupPerFeature = true; } osg::Group* group = new osg::Group; for (int i = 0; i < file->GetLayerCount(); i++) { osg::Group* node = readLayer(file->GetLayer(i), file->GetName(), useRandomColorByFeature, addGroupPerFeature); if (node) group->addChild( node ); } OGRDataSource::DestroyDataSource( file ); return group; }
bool V2vProj::Compute(const data::VectorBarral * barrel) { OGRDataSource * poSourceDs = VectorOpen(barrel->GetSrcDataSource().c_str(), GA_ReadOnly); ON_SCOPE_EXIT([&]() {OGRDataSource::DestroyDataSource(poSourceDs); }); OGRDataSource * poOutputDs = VectorOpen(barrel->GetDstDataSource().c_str(), GA_Update); ON_SCOPE_EXIT([&]() {OGRDataSource::DestroyDataSource(poOutputDs); }); OGRLayer * poSrcLayer = poSourceDs->GetLayerByName( barrel->GetSrcLayer().c_str()); OGRLayer * poDstLayer = poOutputDs->GetLayerByName( barrel->GetDstLayer().c_str()); OGRSpatialReference * poSourceSRS = poSrcLayer->GetSpatialRef(); OGRCoordinateTransformation * poCT = poCT = OGRCreateCoordinateTransformation( poSourceSRS, m_ogrSr); OGRFeatureDefn * poDstFeatureDefn = poDstLayer->GetLayerDefn(); auto features = barrel->GetFeatures(); std::for_each(begin(features), end(features) , [&](int fid) { poSrcLayer->GetFeature(fid); OGRFeature * poDstFeature = OGRFeature::CreateFeature(poDstFeatureDefn); ON_SCOPE_EXIT([&]() {OGRFeature::DestroyFeature(poDstFeature); }); poDstFeature->SetFrom(poSrcLayer->GetFeature(fid)); OGRGeometry * poDstGeometry = poDstFeature->GetGeometryRef(); OGRGeometry * poReprojectedGeom = OGRGeometryFactory::transformWithOptions( poDstGeometry, poCT, NULL); poDstFeature->SetGeometryDirectly(poReprojectedGeom); poDstLayer->CreateFeature(poDstFeature); }); return true; }
//#include "s57.h" int main(int argc, char **argv) { OGRRegisterAll(); OGRDataSource *poDS; printf("Opening %s\n",argv[1]); poDS = OGRSFDriverRegistrar::Open( argv[1], FALSE ); if( poDS == NULL ) { printf( "Open failed.\n" ); exit( 1 ); } OGRLayer *poLayer; int layers = poDS->GetLayerCount(); for (int layer =0 ; layer< layers; layer++) { poLayer = poDS->GetLayer(layer); if (poLayer == NULL) continue; printf("%d, %s, %s",layer, poLayer->GetName(), OGRGeometryTypeToName(poLayer->GetGeomType())); poLayer->ResetReading(); OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn(); int iField; for( iField = 0; iField < poFDefn->GetFieldCount(); iField++ ) { OGRFieldDefn *poFieldDefn = poFDefn->GetFieldDefn( iField ); printf(", %s",poFieldDefn->GetNameRef()); } printf("\n"); } }
JNIEXPORT jlong JNICALL Java_es_gva_cit_jogr_OGRDataSource_createLayerNat (JNIEnv *env, jobject obj, jlong cPtr, jstring pszName, jlong poSpatialRef, jstring eGType, jobjectArray papszOptions){ OGRDataSource *ds = (OGRDataSource *) 0 ; int longitud; char **opciones; OGRLayer *layer_dstno; OGRSpatialReference *spatialRef; long ptr_dtno=-1; OGRwkbGeometryType geomtype; ds = *(OGRDataSource **)&cPtr; spatialRef = *(OGRSpatialReference **)&poSpatialRef; if(ds!=NULL){ longitud = env->GetArrayLength( papszOptions); opciones = (char **)malloc(sizeof(char *)*longitud); for(int i=0;i<longitud;i++){ jstring el = (jstring)env->GetObjectArrayElement(papszOptions,i); const char *simple_option = env->GetStringUTFChars( el, 0); opciones[i]=(char *)malloc(strlen(simple_option)); strcpy(opciones[i],simple_option); env->ReleaseStringUTFChars( el, simple_option); } const char *type = env->GetStringUTFChars( eGType, 0); const char *name = env->GetStringUTFChars( pszName, 0); if(strcmp(type,"wkbUnknown")==0)geomtype = wkbUnknown; else if(strcmp(type,"wkbPoint")==0)geomtype = wkbPoint; else if(strcmp(type,"wkbLineString")==0)geomtype = wkbLineString; else if(strcmp(type,"wkbPolygon")==0)geomtype = wkbPolygon; else if(strcmp(type,"wkbMultiPoint")==0)geomtype = wkbMultiPoint; else if(strcmp(type,"wkbMultiLineString")==0)geomtype = wkbMultiLineString; else if(strcmp(type,"wkbMultiPolygon")==0)geomtype = wkbMultiPolygon; else if(strcmp(type,"wkbGeometryCollection")==0)geomtype = wkbGeometryCollection; else if(strcmp(type,"wkbNone")==0)geomtype = wkbNone; else if(strcmp(type,"wkbLinearRing")==0)geomtype = wkbLinearRing; else if(strcmp(type,"wkbPoint25D")==0)geomtype = wkbPoint25D; else if(strcmp(type,"wkbLineString25D")==0)geomtype = wkbLineString25D; else if(strcmp(type,"wkbPolygon25D")==0)geomtype = wkbPolygon25D; else if(strcmp(type,"wkbMultiPoint25D")==0)geomtype = wkbMultiPoint25D; else if(strcmp(type,"wkbMultiLineString25D")==0)geomtype = wkbMultiLineString25D; else if(strcmp(type,"wkbMultiPolygon25D")==0)geomtype = wkbMultiPolygon25D; else if(strcmp(type,"wkbGeometryCollection25D")==0)geomtype = wkbGeometryCollection25D; layer_dstno = ds->CreateLayer(name, spatialRef, geomtype, opciones); env->ReleaseStringUTFChars( eGType, type); env->ReleaseStringUTFChars( pszName, name); } for(int i=0;i<longitud;i++)free(opciones[i]); free(opciones); if(layer_dstno==NULL)return -1; ptr_dtno = (long)&(*layer_dstno); return (jlong)ptr_dtno; }
GDALDataset* OGRSFDriverRegistrar::OpenWithDriverArg(GDALDriver* poDriver, GDALOpenInfo* poOpenInfo) { OGRDataSource* poDS = (OGRDataSource*) ((OGRSFDriver*)poDriver)->Open(poOpenInfo->pszFilename, poOpenInfo->eAccess == GA_Update); if( poDS != NULL ) poDS->SetDescription( poDS->GetName() ); return poDS; }
GDALDataset* OGRSFDriverRegistrar::CreateVectorOnly( GDALDriver* poDriver, const char * pszName, char ** papszOptions ) { OGRDataSource* poDS = (OGRDataSource*) ((OGRSFDriver*)poDriver)->CreateDataSource(pszName, papszOptions); if( poDS != NULL && poDS->GetName() != NULL ) poDS->SetDescription( poDS->GetName() ); return poDS; }
int OGRDataSource::GetSummaryRefCount() const { int nSummaryCount = m_nRefCount; int iLayer; OGRDataSource *poUseThis = (OGRDataSource *) this; for( iLayer=0; iLayer < poUseThis->GetLayerCount(); iLayer++ ) nSummaryCount += poUseThis->GetLayer( iLayer )->GetRefCount(); return nSummaryCount; }
JNIEXPORT jint JNICALL Java_es_gva_cit_jogr_OGRDataSource_deleteLayerNat (JNIEnv *env, jobject obj, jlong cPtr, jint layer){ OGRDataSource *ds = (OGRDataSource *) 0 ; int ogrerr; ds = *(OGRDataSource **)&cPtr; if(ds!=NULL){ ogrerr = ds->DeleteLayer(layer); } return ogrerr; }
JNIEXPORT jint JNICALL Java_es_gva_cit_jogr_JNIBase_getLayerCountNat (JNIEnv *env, jobject obj, jlong cPtr){ int res=-1; OGRDataSource *ds = (OGRDataSource *) 0 ; ds = *(OGRDataSource **)&cPtr; if(ds!=NULL) res = ds->GetLayerCount(); return res; }
JNIEXPORT jint JNICALL Java_es_gva_cit_jogr_OGRDataSource_syncToDiskNat (JNIEnv *env, jobject obj, jlong cPtr){ OGRDataSource *ds = (OGRDataSource *) 0 ; int ogrerr; ds = *(OGRDataSource **)&cPtr; if(ds!=NULL ){ ogrerr = ds->SyncToDisk(); } return ogrerr; }
JNIEXPORT void JNICALL Java_es_gva_cit_jogr_OGRDataSource_releaseResultSetNat (JNIEnv *env, jobject obj, jlong cPtr, jlong ptr_poResultsSet){ OGRDataSource *ds = (OGRDataSource *) 0 ; OGRLayer *layer = (OGRLayer *) 0 ; ds = *(OGRDataSource **)&cPtr; layer = *(OGRLayer **)&ptr_poResultsSet; if(ds!=NULL ){ ds->ReleaseResultSet(layer); } }
int OGRDataSource::GetSummaryRefCount() const { CPLMutexHolderD( (void **) &m_hMutex ); int nSummaryCount = m_nRefCount; int iLayer; OGRDataSource *poUseThis = (OGRDataSource *) this; for( iLayer=0; iLayer < poUseThis->GetLayerCount(); iLayer++ ) nSummaryCount += poUseThis->GetLayer( iLayer )->GetRefCount(); return nSummaryCount; }
OGRDataSourceH OGR_Dr_Open( OGRSFDriverH hDriver, const char *pszName, int bUpdate ) { VALIDATE_POINTER1( hDriver, "OGR_Dr_Open", NULL ); OGRDataSource *poDS = ((OGRSFDriver *)hDriver)->Open( pszName, bUpdate ); if( poDS != NULL && poDS->GetDriver() == NULL ) poDS->SetDriver( (OGRSFDriver *)hDriver ); return (OGRDataSourceH) poDS; }
JNIEXPORT jint JNICALL Java_es_gva_cit_jogr_JNIBase_dereferenceNat (JNIEnv *env, jobject obj, jlong cPtr){ OGRDataSource *ds = (OGRDataSource *) 0 ; int res=-1; ds = *(OGRDataSource **)&cPtr; if(ds!=NULL){ res = ds->Dereference(); } return res; }
JNIEXPORT jint JNICALL Java_es_gva_cit_jogr_JNIBase_getSummaryRefCountNat (JNIEnv *env, jobject obj, jlong cPtr){ OGRDataSource *ds = (OGRDataSource *) 0 ; int res = -1; ds = *(OGRDataSource **)&cPtr; if(ds!=NULL){ res = ds->GetSummaryRefCount(); } return res; }
OGRDataSource * VectorCreate( const char * pszFormat, const char * pszFilename, char ** papszOptions/*=NULL*/ ) { OGRSFDriver * poDriver = GetVectorDriver(pszFormat); OGRDataSource * poOGRDataSource = poDriver->CreateDataSource(pszFilename, papszOptions); #ifdef TRACEON //测试时使用文件型数据库,没有图层时打开有问题,因此在这里放一个图层 //这样才能打开,不用测试可以关闭,在release中没有 poOGRDataSource->CreateLayer("TEMP", NULL, wkbUnknown, NULL); #endif return poOGRDataSource; }
JNIEXPORT jlong JNICALL Java_es_gva_cit_jogr_OGRDataSource_getStyleTableNat (JNIEnv *env, jobject obj, jlong cPtr){ OGRDataSource *ds = (OGRDataSource *) 0 ; OGRStyleTable *styletable; long ptro_styletable=-1; ds = *(OGRDataSource **)&cPtr; if(ds!=NULL){ styletable = ds->GetStyleTable(); if(styletable!=NULL) ptro_styletable = (long)&(*styletable); } return ptro_styletable; }
OGRLayer *OGRDataSourceWithTransaction::CopyLayer( OGRLayer *poSrcLayer, const char *pszNewName, char **papszOptions ) { if( !m_poBaseDataSource ) return NULL; return WrapLayer(m_poBaseDataSource->CopyLayer(poSrcLayer, pszNewName, papszOptions )); }
CPLErr OGRDataSourceWithTransaction::SetMetadataItem( const char * pszName, const char * pszValue, const char * pszDomain ) { if( !m_poBaseDataSource ) return CE_Failure; return m_poBaseDataSource->SetMetadataItem(pszName, pszValue, pszDomain); }
JNIEXPORT jint JNICALL Java_es_gva_cit_jogr_OGRDataSource_testCapabilityNat (JNIEnv *env, jobject obj, jlong cPtr, jstring cap){ OGRDataSource *ds = (OGRDataSource *) 0 ; int res=-1; const char *capability; ds = *(OGRDataSource **)&cPtr; if(ds!=NULL){ capability = env->GetStringUTFChars( cap, 0 ); res = ds->TestCapability(capability); env->ReleaseStringUTFChars( cap, capability ); } return res; }
JNIEXPORT jlong JNICALL Java_es_gva_cit_jogr_OGRDataSource_getLayerNat (JNIEnv *env, jobject obj, jlong cPtr, jint iLayer){ OGRDataSource *ds = (OGRDataSource *) 0 ; OGRLayer *capa; long layer=-1; ds = *(OGRDataSource **)&cPtr; if(ds!=NULL){ capa = ds->GetLayer(iLayer); if(capa!=NULL)layer = (long)&(*capa); } return (jlong)layer; }
JNIEXPORT jstring JNICALL Java_es_gva_cit_jogr_OGRDataSource_getNameNat (JNIEnv *env, jobject obj, jlong cPtr){ OGRDataSource *ds = (OGRDataSource *) 0 ; jstring nom_ds; ds = *(OGRDataSource **)&cPtr; const char *name = ds->GetName(); if(name!=NULL) nom_ds = env->NewStringUTF(name); else return NULL; return nom_ds; }
OGRLayer* import_shapefile(string shapefile_name, int layer_number) { OGRRegisterAll(); OGRDataSource *poDS; poDS = OGRSFDriverRegistrar::Open( shapefile_name.c_str(), FALSE ); if( poDS == NULL ) { cerr << "Failed to open shapefile" << endl; exit( 1 ); } OGRLayer* poLayer = poDS->GetLayer(layer_number); cerr << "Shapefile layer name: " << poLayer->GetName() << endl; // char* proj[255]; // poLayer->GetSpatialRef()->exportToWkt(proj); // cerr << "Shapefile projection: " << *proj << endl; return poLayer; }
/** @brief Get list of shapes from file, optional search by name * * read in shape file, extract shapes return as list * optional string argument returns only matching shapes (by string.find) * (!case sensitive!) */ void getAvailShapes(vector<OGRFeature*>* availShapes_p, const string selected = "") { OGRRegisterAll(); OGRDataSource * poDS; string path = "../admin_level_6/shape_al6/admin_level_6.shp"; if (exists(path)) { poDS = OGRSFDriverRegistrar::Open(path.c_str()); if (poDS == NULL) { cout << "Data read error" << endl; exit (1); } } else { cout << "File not found" << endl; exit(1); } // cout << "Found " << poDS->GetLayerCount() << "Layers in File" << endl; OGRLayer *poLayer; OGRFeature *poFeature; poLayer = poDS->GetLayer(0); poLayer->ResetReading(); while ( (poFeature = poLayer->GetNextFeature()) != NULL) { if (selected.empty()) { availShapes_p->push_back(poFeature); } else { if (string(poFeature->GetFieldAsString(1)).find(selected) != string::npos) availShapes_p->push_back(poFeature); else OGRFeature::DestroyFeature(poFeature); } } OGRDataSource::DestroyDataSource(poDS); }
void MSN_Helper::LoadSamples(Config &config, Matrix &mat) { if(config.bShapefile) { OGRRegisterAll(); string pLayerName = StringGetFileName(config.sSamples); OGRDataSource* poDS = OGRSFDriverRegistrar::Open(config.sSamples.c_str(),FALSE); OGRLayer* poLayer = poDS->GetLayerByName(pLayerName.c_str()); config.nSamples = poLayer->GetFeatureCount(); mat.Resize(config.nSamples, 4); OGRPoint *poPoint; OGRFeature * pFeature =poLayer->GetNextFeature(); for(unsigned long i=1; i<=config.nSamples; i++) { //样本取值字段名为value,double型 poPoint = (OGRPoint *)pFeature->GetGeometryRef(); mat[i][1] = poPoint->getX(); mat[i][2] = poPoint->getY(); mat[i][3] = pFeature->GetFieldAsInteger("stratum"); mat[i][4] = pFeature->GetFieldAsDouble("value"); pFeature = poLayer->GetNextFeature(); } OGRDataSource::DestroyDataSource( poDS ); } else { double x, y, v, stratum; string sline; ifstream infile(config.sSamples.c_str()); infile >> config.nSamples; mat.Resize(config.nSamples, 4); for(unsigned long i=1; i<=config.nSamples; i++) { infile >> x >> y >> stratum >> v; mat[i][1] = x; mat[i][2] = y; mat[i][3] = stratum; mat[i][4] = v; } infile.close(); } }
JNIEXPORT jlong JNICALL Java_es_gva_cit_jogr_OGRDataSource_getLayerByNameNat (JNIEnv *env, jobject obj, jlong cPtr, jstring name){ OGRDataSource *ds = (OGRDataSource *) 0 ; const char *layername; OGRLayer *layer; long ptro_layer=-1; ds = *(OGRDataSource **)&cPtr; layername = env->GetStringUTFChars( name, 0 ); if(ds!=NULL){ layer=ds->GetLayerByName(layername); if(layer!=NULL)ptro_layer = (long)&(*layer); } env->ReleaseStringUTFChars( name, layername ); return (jlong)ptro_layer; }
OGRLayer *OGRDataSourceWithTransaction::ICreateLayer( const char *pszName, OGRSpatialReference *poSpatialRef, OGRwkbGeometryType eGType, char ** papszOptions) { if( !m_poBaseDataSource ) return NULL; return WrapLayer(m_poBaseDataSource->CreateLayer(pszName, poSpatialRef, eGType, papszOptions)); }