本文整理汇总了C++中OGRDataSource::CreateLayer方法的典型用法代码示例。如果您正苦于以下问题:C++ OGRDataSource::CreateLayer方法的具体用法?C++ OGRDataSource::CreateLayer怎么用?C++ OGRDataSource::CreateLayer使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类OGRDataSource
的用法示例。
在下文中一共展示了OGRDataSource::CreateLayer方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: ProblemReporterOGR
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'");
}
}
示例2: if
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;
}
示例3: WrapLayer
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));
}
示例4: VectorCreate
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;
}
示例5: init_layer
OGRLayer* init_layer(const std::string& name, const stringv& fields, const OGRwkbGeometryType type) {
std::cerr << "Creating layer: " << name << "\n";
OGRLayer* layer = m_data_source->CreateLayer(name.c_str(), &m_srs_out, type, NULL);
if (layer == NULL) {
std::cerr << "Layer creation failed (" << name << ").\n";
exit(1);
}
std::cerr << " Creating field: osm_id\n";
OGRFieldDefn field_osm_id("osm_id", OFTString);
field_osm_id.SetWidth(11);
if (layer->CreateField(&field_osm_id) != OGRERR_NONE ) {
std::cerr << "Creating field 'osm_id' failed.\n";
exit(1);
}
std::cerr << " Creating field: z_order\n";
OGRFieldDefn field_z_order("z_order", OFTInteger);
field_z_order.SetWidth(4);
if (layer->CreateField(&field_z_order) != OGRERR_NONE ) {
std::cerr << "Creating field 'z_order' failed.\n";
exit(1);
}
std::cerr << " Creating field: way_area\n";
OGRFieldDefn field_way_area("way_area", OFTReal);
field_way_area.SetWidth(10);
if (layer->CreateField(&field_way_area) != OGRERR_NONE ) {
std::cerr << "Creating field 'way_area' failed.\n";
exit(1);
}
stringv::const_iterator it;
for (it = fields.begin(); it != fields.end(); ++it) {
std::cerr << " Creating field: " << *it << "\n";
OGRFieldDefn field_tag((*it).c_str(), OFTString);
field_tag.SetWidth(255);
if (layer->CreateField(&field_tag) != OGRERR_NONE ) {
std::cerr << "Creating field '" << *it << "' failed.\n";
exit(1);
}
}
return layer;
}
示例6: writePolygons
void ShapefileWriter::writePolygons(shared_ptr<const OsmMap> map, const QString& path)
{
OGRRegisterAll();
_removeShapefile(path);
const char *pszDriverName = "ESRI Shapefile";
OGRSFDriver *poDriver = OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(pszDriverName);
if( poDriver == NULL )
{
throw HootException(QString("%1 driver not available.").arg(pszDriverName));
}
OGRDataSource* poDS = poDriver->CreateDataSource(path.toAscii(), NULL );
if( poDS == NULL )
{
throw HootException(QString("Data source creation failed. %1").arg(path));
}
OgrOptions options;
options["ENCODING"] = "UTF-8";
OGRLayer *poLayer;
QString layerName;
layerName = QFileInfo(path).baseName();
poLayer = poDS->CreateLayer(layerName.toAscii(),
map->getProjection().get(), wkbMultiPolygon,
options.getCrypticOptions());
if( poLayer == NULL )
{
throw HootException(QString("Layer creation failed. %1").arg(path));
}
QStringList shpColumns;
QStringList columns = getColumns(map, ElementType::Unknown);
for (int i = 0; i < columns.size(); i++)
{
OGRFieldDefn oField(columns[i].toAscii(), OFTString);
oField.SetWidth(64);
if( poLayer->CreateField( &oField ) != OGRERR_NONE )
{
throw HootException(QString("Error creating field (%1).").arg(columns[i]));
}
shpColumns.append(poLayer->GetLayerDefn()->GetFieldDefn(i)->GetNameRef());
}
if (_includeInfo)
{
OGRFieldDefn oField("error_circ", OFTReal);
if( poLayer->CreateField( &oField ) != OGRERR_NONE )
{
throw HootException(QString("Error creating field (error:circular)."));
}
_circularErrorIndex = poLayer->GetLayerDefn()->GetFieldCount() - 1;
}
const WayMap& ways = map->getWays();
for (WayMap::const_iterator it = ways.begin(); it != ways.end(); it++)
{
shared_ptr<Way> way = it->second;
if (OsmSchema::getInstance().isArea(way))
{
_writeWayPolygon(map, way, poLayer, columns, shpColumns);
}
}
const RelationMap& relations = map->getRelationMap();
for (RelationMap::const_iterator it = relations.begin(); it != relations.end(); it++)
{
shared_ptr<Relation> relation = it->second;
if (relation->isMultiPolygon())
{
_writeRelationPolygon(map, relation, poLayer, columns, shpColumns);
}
}
OGRDataSource::DestroyDataSource(poDS);
}
示例7: writePoints
void ShapefileWriter::writePoints(shared_ptr<const OsmMap> map, const QString& path)
{
OGRRegisterAll();
_removeShapefile(path);
const char *pszDriverName = "ESRI Shapefile";
OGRSFDriver *poDriver = OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(pszDriverName);
if( poDriver == NULL )
{
throw HootException(QString("%1 driver not available.").arg(pszDriverName));
}
OGRDataSource* poDS = poDriver->CreateDataSource(path.toAscii(), NULL );
if( poDS == NULL )
{
throw HootException(QString("Data source creation failed. %1").arg(path));
}
OgrOptions options;
options["ENCODING"] = "UTF-8";
OGRLayer *poLayer;
QString layerName;
layerName = QFileInfo(path).baseName();
poLayer = poDS->CreateLayer(layerName.toAscii(),
map->getProjection().get(), wkbPoint, options.getCrypticOptions());
if( poLayer == NULL )
{
throw HootException(QString("Layer creation failed. %1").arg(path));
}
QStringList shpColumns;
QStringList columns = getColumns(map, ElementType::Node);
for (int i = 0; i < columns.size(); i++)
{
OGRFieldDefn oField(columns[i].toAscii(), OFTString);
oField.SetWidth(64);
if( poLayer->CreateField( &oField ) != OGRERR_NONE )
{
throw HootException(QString("Error creating field (%1).").arg(columns[i]));
}
shpColumns.append(poLayer->GetLayerDefn()->GetFieldDefn(i)->GetNameRef());
}
if (_includeInfo)
{
OGRFieldDefn oField("error_circ", OFTReal);
if( poLayer->CreateField( &oField ) != OGRERR_NONE )
{
throw HootException(QString("Error creating field (error:circular)."));
}
_circularErrorIndex = poLayer->GetLayerDefn()->GetFieldCount() - 1;
}
const NodeMap& nodes = map->getNodeMap();
for (NodeMap::const_iterator it = nodes.begin(); it != nodes.end(); ++it)
{
const shared_ptr<Node>& node = it->second;
if (node->getTags().getNonDebugCount() > 0)
{
OGRFeature* poFeature = OGRFeature::CreateFeature( poLayer->GetLayerDefn() );
// set all the column values.
for (int i = 0; i < columns.size(); i++)
{
if (node->getTags().contains(columns[i]))
{
QByteArray c = shpColumns[i].toAscii();
QByteArray v = node->getTags()[columns[i]].toUtf8();
poFeature->SetField(c.constData(), v.constData());
}
}
if (_includeInfo)
{
poFeature->SetField(_circularErrorIndex, node->getCircularError());
}
// convert the geometry.
OGRGeometry* geom = new OGRPoint(node->getX(), node->getY());
if (poFeature->SetGeometryDirectly(geom) != OGRERR_NONE)
{
throw HootException(QString("Error setting geometry"));
}
if (poLayer->CreateFeature(poFeature) != OGRERR_NONE)
{
throw HootException(QString("Error creating feature"));
}
OGRFeature::DestroyFeature(poFeature);
}
}
//.........这里部分代码省略.........
示例8: writeLines
void ShapefileWriter::writeLines(shared_ptr<const OsmMap> map, const QString& path)
{
OGRRegisterAll();
_removeShapefile(path);
const char *pszDriverName = "ESRI Shapefile";
OGRSFDriver *poDriver = OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(pszDriverName);
if( poDriver == NULL )
{
throw HootException(QString("%1 driver not available.").arg(pszDriverName));
}
OGRDataSource* poDS = poDriver->CreateDataSource(path.toAscii(), NULL );
if( poDS == NULL )
{
throw HootException(QString("Data source creation failed. %1").arg(path));
}
OGRLayer *poLayer;
OgrOptions options;
options["ENCODING"] = "UTF-8";
QString layerName;
layerName = QFileInfo(path).baseName();
poLayer = poDS->CreateLayer(layerName.toAscii(),
map->getProjection().get(), wkbLineString,
options.getCrypticOptions());
if( poLayer == NULL )
{
throw HootException(QString("Layer creation failed. %1").arg(path));
}
QStringList shpColumns;
QStringList columns = getColumns(map, ElementType::Way);
for (int i = 0; i < columns.size(); i++)
{
OGRFieldDefn oField(columns[i].toAscii(), OFTString);
oField.SetWidth(64);
if( poLayer->CreateField( &oField ) != OGRERR_NONE )
{
throw HootException(QString("Error creating field (%1).").arg(columns[i]));
}
shpColumns.append(poLayer->GetLayerDefn()->GetFieldDefn(i)->GetNameRef());
}
if (_includeInfo)
{
OGRFieldDefn oField("error_circ", OFTReal);
if( poLayer->CreateField( &oField ) != OGRERR_NONE )
{
throw HootException(QString("Error creating field (error:circular)."));
}
_circularErrorIndex = poLayer->GetLayerDefn()->GetFieldCount() - 1;
}
const WayMap& ways = map->getWays();
for (WayMap::const_iterator it = ways.begin(); it != ways.end(); it++)
{
shared_ptr<Way> way = it->second;
if (OsmSchema::getInstance().isArea(way) == false)
{
OGRFeature* poFeature = OGRFeature::CreateFeature( poLayer->GetLayerDefn() );
// set all the column values.
for (int i = 0; i < columns.size(); i++)
{
if (way->getTags().contains(columns[i]))
{
QByteArray c = shpColumns[i].toAscii();
QByteArray v = way->getTags()[columns[i]].toUtf8();
poFeature->SetField(c.constData(), v.constData());
}
}
if (_includeInfo)
{
poFeature->SetField(_circularErrorIndex, way->getCircularError());
}
// convert the geometry.
std::string wkt = ElementConverter(map).convertToLineString(way)->toString();
char* t = (char*)wkt.data();
OGRGeometry* geom;
if (OGRGeometryFactory::createFromWkt(&t, poLayer->GetSpatialRef(), &geom) != OGRERR_NONE)
{
throw HootException(QString("Error parsing WKT (%1)").arg(QString::fromStdString(wkt)));
}
if (poFeature->SetGeometryDirectly(geom) != OGRERR_NONE)
{
throw HootException(QString("Error setting geometry"));
}
if (poLayer->CreateFeature(poFeature) != OGRERR_NONE)
//.........这里部分代码省略.........
示例9: main
int main(int argc, char **argv)
{
// Get data from ogr
OGRRegisterAll();
std::cout << "Opening: " << argv[1] << std::endl;
OGRDataSource *shp = OGRSFDriverRegistrar::Open(argv[1], FALSE);
IsValid(shp, "Error opening file.");
std::cout << "Shape contains " << shp->GetLayerCount() << " layers." << std::endl;
OGRLayer *layer = shp->GetLayerByName(argv[2]);
IsValid(layer, "Couldn't grab layer");
OGRSpatialReference *srcSRS = NULL;
srcSRS = layer->GetSpatialRef();
// Set up writing
const char *kDriverName = "ESRI Shapefile";
OGRSFDriver *shpDriver = OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(kDriverName);
IsValid(shpDriver, "Couldn't grab the shapefile driver.");
IsValid(argv[3], "Please provide a output shp.");
std::cout << "Writing to: " << argv[3] << std::endl;
OGRDataSource *shpOut = shpDriver->CreateDataSource(argv[3], NULL);
IsValid(shpOut, "Couldn't open output file");
OGRLayer *outLayer = shpOut->CreateLayer(layer->GetName(), srcSRS, wkbMultiLineString, NULL);
IsValid(outLayer, "Couldn't create an output layer");
// copy over the fields from the source file
OGRFeatureDefn *source = layer->GetLayerDefn();
for(int i=0; i < source->GetFieldCount(); i++){
OGRFieldDefn *field = source->GetFieldDefn(i);
if(outLayer->CreateField(field) != OGRERR_NONE) {
std::cout << "Couldn't make layer" << std::endl; exit(1);
};
}
// Loop through features and grab the hull and put it into CGAL then
// skeletonize the points
OGRFeature *feature;
int count = 0;
while((feature = layer->GetNextFeature()) != NULL)
{
OGRMultiPolygon *geometry = dynamic_cast<OGRMultiPolygon *>(OGRGeometryFactory::forceToMultiPolygon(feature->GetGeometryRef()));
IsValid(geometry, "No geometry.");
OGRFeature *outFeature = OGRFeature::CreateFeature(outLayer->GetLayerDefn());
IsValid(outFeature, "Couldn't make a feature.");
for(int i=0; i < source->GetFieldCount(); i++){
OGRField *field = feature->GetRawFieldRef(i);
outFeature->SetField(i, field);
}
OGRGeometry* line = NULL;
for(int i=0; i < geometry->getNumGeometries(); i++){
OGRGeometry* segment = BuildMultiLine(geometry->getGeometryRef(i));
if(segment != NULL){
if(line == NULL) { line = new OGRLineString; }
OGRGeometry* tmp = line->Union(segment);
if(tmp != NULL){
delete line;
line = tmp;
}
delete segment;
}
}
outFeature->SetGeometry(line);
if(outLayer->CreateFeature(outFeature) != OGRERR_NONE){
std::cout << "Couldn't create feature." << std::endl;
exit(1);
}
// clean up
OGRFeature::DestroyFeature(outFeature);
std::cout << std::endl << ++count << std::endl;
}
// cleanup
OGRDataSource::DestroyDataSource(shp);
OGRDataSource::DestroyDataSource(shpOut);
return 0;
}
示例10: layer_point_field_id
MyOGRHandler(const std::string& driver_name, const std::string& filename) {
OGRRegisterAll();
OGRSFDriver* driver = OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(driver_name.c_str());
if (!driver) {
std::cerr << driver_name << " driver not available.\n";
exit(1);
}
CPLSetConfigOption("OGR_SQLITE_SYNCHRONOUS", "FALSE");
const char* options[] = { "SPATIALITE=TRUE", nullptr };
m_data_source = driver->CreateDataSource(filename.c_str(), const_cast<char**>(options));
if (!m_data_source) {
std::cerr << "Creation of output file failed.\n";
exit(1);
}
OGRSpatialReference sparef;
sparef.importFromProj4(m_factory.proj_string().c_str());
m_layer_point = m_data_source->CreateLayer("postboxes", &sparef, wkbPoint, nullptr);
if (!m_layer_point) {
std::cerr << "Layer creation failed.\n";
exit(1);
}
OGRFieldDefn layer_point_field_id("id", OFTReal);
layer_point_field_id.SetWidth(10);
if (m_layer_point->CreateField(&layer_point_field_id) != OGRERR_NONE) {
std::cerr << "Creating id field failed.\n";
exit(1);
}
OGRFieldDefn layer_point_field_operator("operator", OFTString);
layer_point_field_operator.SetWidth(30);
if (m_layer_point->CreateField(&layer_point_field_operator) != OGRERR_NONE) {
std::cerr << "Creating operator field failed.\n";
exit(1);
}
/* Transactions might make things faster, then again they might not.
Feel free to experiment and benchmark and report back. */
m_layer_point->StartTransaction();
m_layer_linestring = m_data_source->CreateLayer("roads", &sparef, wkbLineString, nullptr);
if (!m_layer_linestring) {
std::cerr << "Layer creation failed.\n";
exit(1);
}
OGRFieldDefn layer_linestring_field_id("id", OFTReal);
layer_linestring_field_id.SetWidth(10);
if (m_layer_linestring->CreateField(&layer_linestring_field_id) != OGRERR_NONE) {
std::cerr << "Creating id field failed.\n";
exit(1);
}
OGRFieldDefn layer_linestring_field_type("type", OFTString);
layer_linestring_field_type.SetWidth(30);
if (m_layer_linestring->CreateField(&layer_linestring_field_type) != OGRERR_NONE) {
std::cerr << "Creating type field failed.\n";
exit(1);
}
m_layer_linestring->StartTransaction();
m_layer_polygon = m_data_source->CreateLayer("buildings", &sparef, wkbMultiPolygon, nullptr);
if (!m_layer_polygon) {
std::cerr << "Layer creation failed.\n";
exit(1);
}
OGRFieldDefn layer_polygon_field_id("id", OFTInteger);
layer_polygon_field_id.SetWidth(10);
if (m_layer_polygon->CreateField(&layer_polygon_field_id) != OGRERR_NONE) {
std::cerr << "Creating id field failed.\n";
exit(1);
}
OGRFieldDefn layer_polygon_field_type("type", OFTString);
layer_polygon_field_type.SetWidth(30);
if (m_layer_polygon->CreateField(&layer_polygon_field_type) != OGRERR_NONE) {
std::cerr << "Creating type field failed.\n";
exit(1);
}
m_layer_polygon->StartTransaction();
}
示例11: setError
OGRDataSource *AoIIntersection::intersectAoIWithLayers ( OGRDataSource *ogrSourceData, OGRPolygon *aoiPoly, IntersectionSummary *summary, const char *outFmt )
{
OGRDataSource *ogrIntersection = NULL;
// Spatial reference setup
// make a spatial reference for the area of interest polygon
OGRSpatialReference aoiRef;
aoiRef.SetWellKnownGeogCS( "WGS84" );
// make a spatial reference for the coord sys we will use to calculate area in acres - Albers USA equal area conic
bool acreageCalcAvailable = true;
char *aecWkt = "PROJCS[\"USA_Contiguous_Lambert_Conformal_Conic\",GEOGCS[\"GCS_North_American_1983\",DATUM[\"North_American_Datum_1983\",SPHEROID[\"GRS_1980\",6378137,298.257222101]],PRIMEM[\"Greenwich\",0],UNIT[\"Degree\",0.017453292519943295]],PROJECTION[\"Lambert_Conformal_Conic_2SP\"],PARAMETER[\"False_Easting\",0],PARAMETER[\"False_Northing\",0],PARAMETER[\"Central_Meridian\",-96],PARAMETER[\"Standard_Parallel_1\",33],PARAMETER[\"Standard_Parallel_2\",45],PARAMETER[\"Latitude_Of_Origin\",39],UNIT[\"Meter\",1],AUTHORITY[\"EPSG\",\"102004\"]]";
OGRSpatialReference aecRef;
OGRErr ogrErr = aecRef.importFromWkt( &aecWkt );
if ( ogrErr != OGRERR_NONE )
{
setError ( NO_SPATIAL_REFERENCE );
acreageCalcAvailable = false;
}
// begin creating the output data structure
// OGRDataSource is the root
ogrIntersection = buildIntersectionDataSource( outFmt );
if (! ogrIntersection )
{
setError( NO_OUTPUT_DATASOURCE );
return 0;
}
int acreIndex = 0, areaIndex = 0;
OGRFieldDefn *acreFldDefn = NULL, *areaPctFldDefn = NULL;
OGRFeatureDefn *featureDefn = buildFeatureDefinition( acreIndex, areaIndex, acreFldDefn, areaPctFldDefn );
// walk the layers in the input data
//
OGRLayer *inputLayer;
summary->numLayers = ogrSourceData->GetLayerCount();
for (int layerCt = 0; layerCt < summary->numLayers; ++layerCt)
{
inputLayer = ogrSourceData->GetLayer( layerCt );
if ( inputLayer == NULL )
{
setError( NO_INPUT_LAYER );
// clean up
delete ogrIntersection;
return 0;
}
// make a clone of aoi polygon to be manipulated
OGRPolygon *aoiClone = (OGRPolygon *)aoiPoly->clone();
if ( ! aoiClone )
{
setError( NO_AOI_CLONE );
// clean up
delete ogrIntersection;
return 0;
}
// ensure that the area of interest polygon is in the same spatial reference as the data layer
// find the spatial reference for the layer
OGRSpatialReference *dataRef = inputLayer->GetSpatialRef();
if ( dataRef )
{
OGRCoordinateTransformation *aoiTransform = OGRCreateCoordinateTransformation( &aoiRef, dataRef );
if( aoiTransform == NULL )
{
setError( NO_AOI_TRANSFORM );
// clean up
delete ogrIntersection;
delete aoiClone;
return 0;
}
aoiClone->transform( aoiTransform );
delete aoiTransform;
}
// find the transform from data layer's CS to Albers USA
// for acreage calculation
OGRCoordinateTransformation *aecTransform = NULL;
acreageCalcAvailable = false;
if ( dataRef )
{
aecTransform = OGRCreateCoordinateTransformation( dataRef, &aecRef );
if( aecTransform == NULL )
{
setError( NO_ACRE_TRANSFORM );
}
else
acreageCalcAvailable = true;
}
// the area enclosed by the AoI
// used for computing the percentage of the AoI intersected by polygons
summary->aoiArea = aoiClone->getExteriorRing()->get_Area();
// create a layer for outputting the intersecting polygons
OGRLayer *intersectionLayer = ogrIntersection->CreateLayer( inputLayer->GetLayerDefn()->GetName(), dataRef, wkbPolygon, 0 );
if ( ! intersectionLayer )
{
//.........这里部分代码省略.........
示例12: ExportToGISFile
void CDlg_GISDataExchange::ExportToGISFile(LPCTSTR lpszCSVFileName,LPCTSTR lpszShapeFileName, CString GISTypeString )
{
#ifndef _WIN64
m_MessageList.ResetContent ();
CWaitCursor wait;
CCSVParser parser;
int i= 0;
// open csv file
if (parser.OpenCSVFile(lpszCSVFileName))
{
CString message_str;
OGRSFDriver *poDriver;
OGRRegisterAll();
poDriver = OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(GISTypeString );
if( poDriver == NULL )
{
message_str.Format ( "%s driver not available.", GISTypeString );
m_MessageList.AddString (message_str);
return;
}
OGRDataSource *poDS;
poDS = poDriver->CreateDataSource(lpszShapeFileName, NULL );
if( poDS == NULL )
{
message_str.Format ( "Creation of GIS output file %s failed.\nPlease do not overwrite the exiting file and please select a new file name.",
lpszShapeFileName );
m_MessageList.AddString (message_str);
return;
}
///// export to link layer
// link layer
OGRLayer *poLayer;
poLayer = poDS->CreateLayer( "link", NULL, wkbLineString, NULL );
if( poLayer == NULL )
{
m_MessageList.AddString ("link Layer creation failed");
return;
}
vector<string> HeaderVector = parser.GetHeaderVector();
std::vector <CString> LongFieldVector;
for(unsigned int i = 0; i < HeaderVector.size(); i++)
{
if(HeaderVector[i].find ("geometry") != string::npos|| HeaderVector[i].find ("name") != string::npos || HeaderVector[i].find ("code") != string::npos)
{
OGRFieldDefn oField (HeaderVector[i].c_str (), OFTString);
CString str;
if( poLayer->CreateField( &oField ) != OGRERR_NONE )
{
str.Format("Creating field %s failed", oField.GetNameRef());
m_MessageList.AddString (str);
return;
}
}else
{
CString field_string = HeaderVector[i].c_str ();
OGRFieldDefn oField (field_string, OFTReal);
CString str;
if( poLayer->CreateField( &oField ) != OGRERR_NONE )
{
str.Format("Creating field %s failed", oField.GetNameRef());
m_MessageList.AddString (str);
return;
}
}
if(HeaderVector[i].size()>=11)
{
LongFieldVector.push_back (HeaderVector[i].c_str ());
}
}
message_str.Format ("%d fields have been created.",HeaderVector.size());
m_MessageList.AddString (message_str);
//.........这里部分代码省略.........
示例13: CacheLayer
bool GdaCache::CacheLayer(std::string ext_ds_name,
OGRLayerProxy* ext_layer_proxy)
{
OGRLayer* poSrcLayer = ext_layer_proxy->layer;
// get information from current layer: geomtype, layer_name
// (NOTE: we don't consider coodinator system and translation here)
OGRFeatureDefn *poSrcFDefn = poSrcLayer->GetLayerDefn();
int eGType = poSrcFDefn->GetGeomType();
const char* pszNewLayerName = poSrcLayer->GetName();
int bForceToPolygon = FALSE;
int bForceToMultiPolygon = FALSE;
int bForceToMultiLineString = FALSE;
if( wkbFlatten(eGType) == wkbPolygon )
bForceToPolygon = TRUE;
else if( wkbFlatten(eGType) == wkbMultiPolygon )
bForceToMultiPolygon = TRUE;
else if( wkbFlatten(eGType) == wkbMultiLineString )
bForceToMultiLineString = TRUE;
//Setup coordinate transformation if we need it.
OGRCoordinateTransformation *poCT = NULL;
bool bTransform = FALSE;
OGRSpatialReference *poSourceSRS = NULL;
// todo
OGRSpatialReference *poOutputSRS = new OGRSpatialReference("EPSG:4326");
// Cache
char *papszLCO[] = {"OVERWRITE=yes","FORMAT=Spatialite"};
std::string cache_layer_name = ext_ds_name + "_"+ext_layer_proxy->name;
OGRDataSource *poDstDS = cach_ds_proxy->ds;
OGRLayer *poDstLayer = poDstDS->CreateLayer(cache_layer_name.c_str(),
poOutputSRS,
(OGRwkbGeometryType)eGType,
papszLCO);
if (poDstLayer == NULL) {
// raise create cache failed.
return false;
}
// Process Layer style table
poDstLayer->SetStyleTable( poSrcLayer->GetStyleTable () );
// Add fields. here to copy all field.
int nSrcFieldCount = poSrcFDefn->GetFieldCount();
int iField;
OGRFeatureDefn *poDstFDefn = poDstLayer->GetLayerDefn();
for( iField = 0; iField < nSrcFieldCount; iField++ )
{
OGRFieldDefn* poSrcFieldDefn = poSrcFDefn->GetFieldDefn(iField);
OGRFieldDefn oFieldDefn( poSrcFieldDefn );
// The field may have been already created at layer creation
if (poDstLayer->CreateField( &oFieldDefn ) == OGRERR_NONE)
{
// now that we've created a field, GetLayerDefn() won't return NULL
if (poDstFDefn == NULL)
poDstFDefn = poDstLayer->GetLayerDefn();
}
}
// Transfer feature from Source Layer to Dest Layer
OGRFeature *poFeature;
GIntBig nFeaturesWritten = 0;
poSrcLayer->ResetReading();
while (poFeature = poSrcLayer->GetNextFeature())
{
OGRFeature *poDstFeature = OGRFeature::CreateFeature(
poDstLayer->GetLayerDefn() );
poDstFeature->SetFrom(poFeature);
OGRGeometry *poDstGeometry = poDstFeature->GetGeometryRef();
if (poDstGeometry != NULL)
{
if( bForceToPolygon )
{
poDstFeature->SetGeometryDirectly(
OGRGeometryFactory::forceToPolygon(
poDstFeature->StealGeometry()));
}
else if( bForceToMultiPolygon )
{
poDstFeature->SetGeometryDirectly(
OGRGeometryFactory::forceToMultiPolygon(
poDstFeature->StealGeometry() ) );
}
else if ( bForceToMultiLineString )
{
poDstFeature->SetGeometryDirectly(
OGRGeometryFactory::forceToMultiLineString(
poDstFeature->StealGeometry() ) );
}
}
if( poDstLayer->CreateFeature( poDstFeature ) == OGRERR_NONE )
{
nFeaturesWritten ++;
}
OGRFeature::DestroyFeature( poDstFeature );
//.........这里部分代码省略.........
示例14: export_
// export
bool ImportExportGdal::export_(const QList<Feature *>& featList)
{
const char *pszDriverName = "SQLite";
OGRSFDriver *poDriver;
OGRRegisterAll();
poDriver = OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(pszDriverName);
if( poDriver == NULL )
{
qDebug( "%s driver not available.", pszDriverName );
return false;
}
OGRDataSource *poDS;
QFile::remove(QString(HOMEDIR + "/test.sqlite"));
poDS = poDriver->CreateDataSource( QString(HOMEDIR + "/test.sqlite").toUtf8().constData(), NULL );
if( poDS == NULL )
{
qDebug( "Creation of output file failed." );
return false;
}
poDS->ExecuteSQL("PRAGMA synchronous = OFF", NULL, NULL);
OGRSpatialReference *poSRS;
poSRS = new OGRSpatialReference();
poSRS->importFromEPSG(4326);
char **papszOptions = NULL;
papszOptions = CSLSetNameValue( papszOptions, "SPATIALITE", "YES" );
papszOptions = CSLSetNameValue( papszOptions, "FORMAT", "SPATIALITE" );
papszOptions = CSLSetNameValue( papszOptions, "SPATIAL_INDEX", "YES" );
OGRLayer *poLayer;
poLayer = poDS->CreateLayer( "osm", poSRS, wkbUnknown, papszOptions);
CSLDestroy( papszOptions );
if( poLayer == NULL )
{
qDebug( "Layer creation failed." );
return false;
}
OGRFieldDefn oField("osm_id", OFTReal);
if( poLayer->CreateField( &oField ) != OGRERR_NONE )
{
qDebug( "Creating field failed." );
return false;
}
oField.Set("osm_version", OFTInteger );
poLayer->CreateField( &oField );
oField.Set("osm_timestamp", OFTInteger );
poLayer->CreateField( &oField );
OGRFeature *poFeature;
foreach (Feature* F, featList) {
poFeature = OGRFeature::CreateFeature( poLayer->GetLayerDefn() );
poFeature->SetField( "osm_id", (qreal)(F->id().numId));
#ifndef FRISIUS_BUILD
poFeature->SetField( "osm_version", F->versionNumber());
poFeature->SetField( "osm_timestamp", (int)F->time().toTime_t());
#endif
if (CHECK_NODE(F)) {
Node* N = STATIC_CAST_NODE(F);
OGRPoint pt;
pt.setX(N->position().x());
pt.setY(N->position().y());
poFeature->SetGeometry( &pt );
} else if (CHECK_WAY(F)) {
Way* W = STATIC_CAST_WAY(F);
OGRLineString ls;
ls.setNumPoints(W->size());
for (int i=0; i<W->size(); ++i) {
ls.setPoint(i, W->getNode(i)->position().x(), W->getNode(i)->position().y(), 0);
}
poFeature->SetGeometry( &ls );
}
if( poLayer->CreateFeature( poFeature ) != OGRERR_NONE )
{
qDebug( "Failed to create feature in output." );
return false;
}
OGRFeature::DestroyFeature( poFeature );
}
示例15: writePolygons
void writePolygons(RegionMap* regionMap)
{
char* driverName = (char *)ogrDriver->c_str();
char* outFilePath = (char *)outFile->c_str();
cout << "Creating output file \"" << outFilePath << "\" (\"" << driverName << "\" format)...";
OGRSFDriver* driver = (OGRSFDriver*)OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(ogrDriver->c_str());
if (driver == NULL) {
localExit(new string(string("FATAL: OGR driver \"") + string(driverName) + string("\" is not available.\n")), 1);
}
char* dataSourceName = NULL;
bool cleanupDataSource = false;
if (ogrDriver->compare("KML") == 0 && kmlStyleFile != NULL) {
dataSourceName = tmpnam(NULL);
//mkstemp(dataSourceName);
cleanupDataSource = true;
}
else if (ogrDriver->compare("ESRI Shapefile") == 0) {
string* dsStr = removeFromLastOccurrence(outFilePath, '/');
dsStr = removeFromLastOccurrence((char *)dsStr->c_str(), '\\');
dataSourceName = (char *)dsStr->c_str();
} else {
dataSourceName = outFilePath;
}
OGRDataSource* ds = driver->CreateDataSource(dataSourceName);
if(ds == NULL) {
localExit(new string(string("FATAL: Unable to create output file \"") + string(outFilePath) + string("\"\n")), 1);
}
cout << "[ok]\n";
OGRCoordinateTransformation* transform = NULL;
const string* rmSpatialRefStr = regionMap->getSpatialRef();
if (rmSpatialRefStr != NULL && rmSpatialRefStr->compare("") != 0) {
OGRSpatialReference* spatialRef = new OGRSpatialReference(rmSpatialRefStr->c_str());
transform = OGRCreateCoordinateTransformation(spatialRef, outSpatialRef);
}
char* layerName = (char *)pointToFilename(outFilePath, true)->c_str();
OGRLayer* layer = ds->CreateLayer(layerName, outSpatialRef, wkbPolygon);
layer->CreateField(new OGRFieldDefn("RegionID", OFTInteger), 0);
layer->CreateField(new OGRFieldDefn("Category", OFTString), 0);
layer->CreateField(new OGRFieldDefn("Threshold", OFTReal), 0);
cout << "Writing polygon data to file...";
int count = 0;
int currRegionID = 0;
int numRegions = regionMap->getNumRegions();
while (currRegionID < numRegions) {
Region *currRegion = regionMap->getRegion(currRegionID);
Polygon* poly = currRegion->toPolygon();
if(poly == NULL) {
currRegionID += 1;
continue;
}
count++;
OGRFeature* feature = new OGRFeature(layer->GetLayerDefn());
feature->SetField(feature->GetFieldIndex("RegionID"), currRegionID);
feature->SetField(feature->GetFieldIndex("Category"), currRegion->getCategory()->getName()->c_str());
feature->SetField(feature->GetFieldIndex("Threshold"), currRegion->getCategory()->minThreshold());
OGRGeometry* geom = BuildOgrPolygon(poly, transform);
feature->SetGeometry(geom);
if (layer->CreateFeature(feature) != OGRERR_NONE)
localExit(new string("ERROR: Unable to create feature for region #\n"), 2);
//OGRFeature::DestroyFeature(feature);
currRegionID += 1;
if (currRegionID % 100 == 0)
cout << ".";
}
//CPLFree(transform);
//CPLFree(layer);
OGRDataSource::DestroyDataSource(ds);
cout << "[ok] Wrote "<< count << " polygons\n";
if(kmlStyleFile != NULL) {
#ifdef USE_XALAN
XALAN_USING_XERCES(XMLPlatformUtils)
XALAN_USING_XALAN(XalanTransformer)
XMLPlatformUtils::Initialize();
XalanTransformer::initialize();
XalanTransformer theXalanTransformer;
cout << "Transforing KML file ..." << outFile->c_str();
if(theXalanTransformer.transform(dataSourceName, kmlStyleFile->c_str(), outFile->c_str()) != 0) {
cout << "[!!]\n";
cout << "ERROR doing XSLT transform using " << kmlStyleFile->c_str() << "\n";
} else {
cout << "...[ok]\n";
}
#endif /* USE_XALAN */
#ifdef USE_LIBXSLT
xsltStylesheetPtr cur = NULL;
xmlDocPtr doc, res;
//.........这里部分代码省略.........