From 53efe44bfd077bd1c3f212bd06cf28f9324ef80d Mon Sep 17 00:00:00 2001 From: Ralf Gerlich Date: Sat, 29 Mar 2008 12:49:37 +0100 Subject: [PATCH] Implemented reading point layers to poly2ogr (was used for debugging calc_point_inside()-stuff) --- src/Utils/poly2ogr/poly2ogr.cxx | 67 +++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/src/Utils/poly2ogr/poly2ogr.cxx b/src/Utils/poly2ogr/poly2ogr.cxx index c88dd1c5..296ab351 100644 --- a/src/Utils/poly2ogr/poly2ogr.cxx +++ b/src/Utils/poly2ogr/poly2ogr.cxx @@ -46,6 +46,7 @@ bool do_split=false; OGRDataSource *datasource; OGRLayer *defaultLayer; +OGRLayer *pointsLayer=NULL; LayerMap layerMap; bool endswith(const std::string& s, const std::string& suffix) { @@ -85,6 +86,34 @@ OGRLayer* create_layer(const std::string& material) { return layer; } +OGRLayer* create_pointsLayer() { + OGRLayer* layer; + + OGRSpatialReference srs; + srs.SetWellKnownGeogCS("WGS84"); + layer=datasource->CreateLayer("points",&srs,wkbPoint,NULL); + if (!layer) { + SG_LOG(SG_GENERAL, SG_ALERT, "Creation of layer 'points' failed"); + return NULL; + } + + OGRFieldDefn materialField("Material", OFTString); + materialField.SetWidth(128); + + OGRFieldDefn fileField("File",OFTString); + fileField.SetWidth(256); + + if( layer->CreateField( &materialField ) != OGRERR_NONE ) { + SG_LOG(SG_GENERAL, SG_ALERT, "Creation of field 'Material' failed"); + } + + if( layer->CreateField( &fileField ) != OGRERR_NONE ) { + SG_LOG(SG_GENERAL, SG_ALERT, "Creation of field 'Material' failed"); + } + + return layer; +} + OGRLayer* get_layer_for_material(const std::string& material) { if (!do_split) { if (!defaultLayer) { @@ -191,6 +220,41 @@ void process_polygon_file(const std::string& path) { } } +void process_points_file(const std::string& path) { + SG_LOG(SG_GENERAL, SG_INFO, "Loading points file " << path); + + sg_gzifstream in( path ); + + if (pointsLayer==NULL) + { + pointsLayer=create_pointsLayer(); + } + + while (!in.eof()) { + std::string material; + double x,y; + in >> x >> y >> material; + + if (in.eof()) + break; + + OGRPoint* point=new OGRPoint(x,y); + + OGRFeature* feature; + feature = new OGRFeature( pointsLayer->GetLayerDefn() ); + feature->SetField("Material", material.c_str()); + feature->SetField("File", path.c_str()); + feature->SetGeometry(point); + + if( pointsLayer->CreateFeature( feature ) != OGRERR_NONE ) + { + SG_LOG(SG_GENERAL, SG_ALERT, "Failed to create feature in shapefile"); + } + + OGRFeature::DestroyFeature(feature); + } +} + void process_file(const std::string& path) { struct stat sbuf; @@ -220,6 +284,9 @@ void process_file(const std::string& path) { } closedir(dir); + } else if (endswith(path,".pts")) { + // This is a points file + process_points_file(path); } else if (!endswith(path,".gz") && !endswith(path,".arr") && !endswith(path,".fit") &&