osg::Geometry* polygonToDrawable(OGRPolygon* polygon) const { osg::Geometry* geom = new osg::Geometry(); osg::Vec3Array* vertices = new osg::Vec3Array(); geom->setVertexArray(vertices); { OGRLinearRing *ring = polygon->getExteriorRing(); OGRPoint point; for(int i = 0; i < ring->getNumPoints(); i++) { ring->getPoint(i, &point); vertices->push_back(osg::Vec3(point.getX(), point.getY(), point.getZ())); } geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_LOOP, 0, vertices->size())); } if (polygon->getNumInteriorRings()) { for (int i = 0; i < polygon->getNumInteriorRings(); i++) { OGRLinearRing *ring = polygon->getInteriorRing(i); OGRPoint point; for (int j = 0; j < ring->getNumPoints(); j++) { ring->getPoint(j, &point); vertices->push_back(osg::Vec3(point.getX(), point.getY(), point.getZ())); } geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_LOOP, vertices->size()-ring->getNumPoints() , ring->getNumPoints())); } } osgUtil::Tessellator tsl; tsl.setTessellationType(osgUtil::Tessellator::TESS_TYPE_GEOMETRY); tsl.setBoundaryOnly(false); tsl.retessellatePolygons(*geom); osg::Vec3Array* array = triangulizeGeometry(geom); geom->setVertexArray(array); geom->removePrimitiveSet(0,geom->getNumPrimitiveSets()); geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::TRIANGLES, 0, array->size())); return geom; }
void MapExtruder::extrusion() { _sides = new OGRMultiPolygon(); int n = _map2d->getNumGeometries(); for(int i=0;i<n;i++) { OGRLinearRing* ring = ((OGRPolygon*)_map2d->getGeometryRef(i))->getExteriorRing(); ring->closeRings(); int nPoints = ring->getNumPoints(); for(int j=0;j<nPoints-1;j++) { OGRPolygon side; OGRLinearRing* ring_side = new OGRLinearRing(); OGRPoint p1,p4; ring->getPoint(j,&p1); ring->getPoint(j+1,&p4); OGRPoint p2(p1.getX(),p1.getY(),_height); OGRPoint p3(p4.getX(),p4.getY(),_height); ring_side->addPoint(&p1); ring_side->addPoint(&p2); ring_side->addPoint(&p3); ring_side->addPoint(&p4); ring_side->addPoint(&p1); side.addRing(ring_side); _sides->addGeometry(&side); } } }
osg::ref_ptr<osg::Group> MapExtruder::osg_assemble() { osg::ref_ptr<osg::Group> osg = new osg::Group; osg::ref_ptr<osg::Node> surface = osgDB::readNodeFile(_map2dFile); ExtrudeVisitor extrude(_height); surface->accept(extrude); osg->addChild(surface); int n = _sides ->getNumGeometries(); osg::ref_ptr<osg::Geode> geode = new osg::Geode; for(int i=0;i<n;i++) { osg::ref_ptr<osg::Geometry> geom = new osg::Geometry; osg::ref_ptr<osg::Vec3Array> verts = new osg::Vec3Array; std::vector<OGRPoint> points; OGRLinearRing *ring = ((OGRPolygon*)_sides->getGeometryRef(i))->getExteriorRing(); int m = ring->getNumPoints(); for(int j=0;j<m;j++) { OGRPoint pt; ring->getPoint(j,&pt); verts->push_back(osg::Vec3(pt.getX(),pt.getY(),pt.getZ())); } geom->setVertexArray(verts); osg::Vec4Array* colors = new osg::Vec4Array; colors->push_back(YELLOW); geom->setColorArray(colors); geom->setColorBinding(osg::Geometry::BIND_OVERALL); geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POLYGON,0,m)); geode->addDrawable(geom); } osg->addChild(geode); return osg; }
PolygonView::PolygonView(MapPolygon* mapPolygon,bool polygonFill,osg::Vec4 color) { this->mapPolygon=mapPolygon; this->geometryEdgePolygon=new osg::Geometry; /*this->geometryPolygon=new osg::Geometry;*/ this->vertices=new osg::Vec3Array; /*this->geometryPolygon->setVertexArray(this->vertices);*/ this->geometryEdgePolygon->setVertexArray(this->vertices); OGRLinearRing *poExteriorRing=mapPolygon->getExteriorRing(); normals=new osg::Vec3Array; normals->push_back(osg::Vec3(0.0f,-1.0f,0.0f)); this->polygonFillColor=new osg::Vec4Array; this->polygonEdgeColor=new osg::Vec4Array; this->polygonFillColor->push_back(color); this->polygonEdgeColor->push_back(edgeColor); { OGRPoint point; for(int i = 0; i < poExteriorRing->getNumPoints(); i++) { poExteriorRing->getPoint(i, &point); vertices->push_back(osg::Vec3(point.getX(), 0, point.getY())); } this->geometryEdgePolygon->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_LOOP, 0, vertices->size())); /*this->geometryPolygon->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_LOOP, 0, vertices->size()));*/ } if (mapPolygon->getNumInteriorRings()) { for (int i = 0; i < mapPolygon->getNumInteriorRings(); i++) { OGRLinearRing *ring = mapPolygon->getInteriorRing(i); OGRPoint point; for (int j = 0; j < ring->getNumPoints(); j++) { ring->getPoint(j, &point); vertices->push_back(osg::Vec3(point.getX(), 0, point.getY())); } this->geometryEdgePolygon->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_LOOP, vertices->size()-ring->getNumPoints() , ring->getNumPoints())); /*this->geometryPolygon->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_LOOP, vertices->size()-ring->getNumPoints() , ring->getNumPoints()));*/ } } this->geometryEdgePolygon->setNormalArray(normals); this->geometryEdgePolygon->setNormalBinding(osg::Geometry::BIND_OVERALL); this->geometryEdgePolygon->setColorArray(this->polygonEdgeColor); this->geometryEdgePolygon->setColorBinding(osg::Geometry::BIND_OVERALL); if (polygonFill==true) { this->geometryPolygon=new osg::Geometry; this->geometryPolygon->setVertexArray(this->vertices); for(int i=0;i<this->geometryEdgePolygon->getNumPrimitiveSets();i++) { this->geometryPolygon->addPrimitiveSet(this->geometryEdgePolygon->getPrimitiveSet(i)); } osgUtil::Tessellator tsl; tsl.setTessellationType(osgUtil::Tessellator::TESS_TYPE_GEOMETRY); tsl.setBoundaryOnly(false); tsl.retessellatePolygons(*this->geometryPolygon); osg::Vec3Array* array = triangulizeGeometry(this->geometryPolygon); this->geometryPolygon->setVertexArray(array); this->geometryPolygon->removePrimitiveSet(0,this->geometryPolygon->getNumPrimitiveSets()); this->geometryPolygon->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::TRIANGLES, 0, array->size())); this->geometryPolygon->setNormalArray(normals); this->geometryPolygon->setNormalBinding(osg::Geometry::BIND_OVERALL); this->geometryPolygon->setColorArray(this->polygonFillColor); this->geometryPolygon->setColorBinding(osg::Geometry::BIND_OVERALL); } }
void CDlg_GISDataExchange::ExportDataToCSV(CString csv_file_name) { #ifndef _WIN64 CString message_str; OGRRegisterAll(); OGRDataSource *poDS; poDS = OGRSFDriverRegistrar::Open(m_GIS_ShapeFile, FALSE ); if( poDS == NULL ) { m_MessageList.AddString("Open file failed." ); return; } ofstream CSVFile; CSVFile.open (csv_file_name, ios::out); if(CSVFile.is_open () == false) { AfxMessageBox("This file cannot be found or opened.\n It might be currently used and locked by EXCEL."); return; }else { CSVFile.width(15); CSVFile.precision(6) ; CSVFile.setf(ios::fixed); } int poLayers = ((OGRDataSource*)poDS)->GetLayerCount() ; for (int i=0; i < poLayers; i++) { OGRLayer *poLayer; poLayer = ((OGRDataSource*)poDS)->GetLayer(i); if(poLayer == NULL) { message_str.Format("Open layer %d failed", i+1); m_MessageList.AddString (message_str); return; } OGRFeature *poFeature; int feature_count = 0; poLayer->ResetReading(); while( (poFeature = poLayer->GetNextFeature()) != NULL ) { OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn(); int iField; if(feature_count == 0) // first feature point, output field name; { for( iField = 0; iField < poFDefn->GetFieldCount(); iField++ ) { OGRFieldDefn *poFieldDefn = poFDefn->GetFieldDefn( iField ); CString str = poFieldDefn->GetNameRef(); str.Replace(" ", NULL); // remove space CSVFile << str << "," ; } CSVFile << "geometry" << endl; } for( iField = 0; iField < poFDefn->GetFieldCount(); iField++ ) { OGRFieldDefn *poFieldDefn = poFDefn->GetFieldDefn( iField ); CString str; if( poFieldDefn->GetType() == OFTInteger ) CSVFile << poFeature->GetFieldAsInteger( iField ) << ","; else if( poFieldDefn->GetType() == OFTReal ) CSVFile << poFeature->GetFieldAsDouble(iField) << ","; else if( poFieldDefn->GetType() == OFTString ) { str = poFeature->GetFieldAsString(iField); if(str.Find(',') >=0) CSVFile << "\"" << poFeature->GetFieldAsString(iField) << "\","; else CSVFile << poFeature->GetFieldAsString(iField) << ","; } else { str = poFeature->GetFieldAsString(iField); if(str.Find(',') >=0) CSVFile << "\"" << poFeature->GetFieldAsString(iField) << "\","; else CSVFile << poFeature->GetFieldAsString(iField) << ","; } } OGRGeometry *poGeometry; poGeometry = poFeature->GetGeometryRef(); if( poGeometry != NULL ) { if(wkbFlatten(poGeometry->getGeometryType()) == wkbPoint ) { OGRPoint *poPoint = (OGRPoint *) poGeometry; CSVFile << "\"<Point><coordinates>" << poPoint->getX() << "," << poPoint->getY() << ",0.0" << "</coordinates></Point>\"" ; } else if (wkbFlatten(poGeometry->getGeometryType()) == wkbLineString) { OGRLineString *poLine = (OGRLineString *) poGeometry; CSVFile << "\"<LineString><coordinates>"; for(unsigned int si = 0; si< poLine->getNumPoints(); si++) { CSVFile << poLine->getX(si) << "," << poLine->getY(si) << ",0.0"; if(si!=poLine->getNumPoints()-1) CSVFile << " "; } CSVFile << "</coordinates></LineString>\","; } if (wkbFlatten(poGeometry->getGeometryType()) == wkbPolygon ) { OGRPolygon* polygon = (OGRPolygon*)(poGeometry); OGRLinearRing *ring = polygon->getExteriorRing(); OGRPoint point; CSVFile << "\"<Polygon><outerBoundaryIs><LinearRing><coordinates>"; for(int i = 0; i < ring->getNumPoints(); i++) { ring->getPoint(i, &point); CSVFile << point.getX() << "," << point.getY() << ",0.0"; if(i!=ring->getNumPoints()-1) CSVFile << " "; } CSVFile << "</coordinates></LinearRing></outerBoundaryIs></Polygon>\""; } CSVFile << endl; } feature_count ++; } OGRFeature::DestroyFeature( poFeature ); message_str.Format("Layer %d has %d features.", i+1, feature_count); m_MessageList.AddString(message_str); } OGRDataSource::DestroyDataSource( poDS ); CSVFile.close(); #endif }
void readQuadIndex(int series, QString file, QString layerName, Projection *pj) { OGRDataSource *ds = OGRSFDriverRegistrar::Open(file.toLatin1().data(), false); if (!ds) { fprintf(stderr, "Could not open quad index '%s'.\n", file.toLatin1().data()); exit(-1); } OGRLayer *layer; layer = ds->GetLayerByName(layerName.toLatin1().data()); if (!layer) { fprintf(stderr, "Could not read layer '%s'.\n", layerName.toLatin1().data()); exit(-1); } OGRSpatialReference *srs = layer->GetSpatialRef(); if (!srs) { fprintf(stderr, "Missing spatial reference for layer '%s'.\n", layerName.toLatin1().data()); exit(-1); } char *proj = NULL; srs->exportToProj4(&proj); if (!proj) { fprintf(stderr, "Error computing PROJ4 spatial reference for layer '%s'.\n", layerName.toLatin1().data()); exit(-1); } Projection pjIndex(proj); CPLFree(proj); layer->ResetReading(); OGRFeatureDefn *def = layer->GetLayerDefn(); int idFieldNr = def->GetFieldIndex("ID"); int nameFieldNr = def->GetFieldIndex("NAME"); if (idFieldNr < 0 || nameFieldNr < 0) { fprintf(stderr, "Missing index layer fields.\n"); exit(-1); } OGRFeature *f; while ((f = layer->GetNextFeature()) != NULL) { QString id(f->GetFieldAsString(idFieldNr)); QString name(f->GetFieldAsString(nameFieldNr)); // printf("Quad id: %s; name: %s\n", id.toLatin1().data(), name.toLatin1().data()); OGRGeometry *g; g = f->GetGeometryRef(); if (g != NULL && wkbFlatten(g->getGeometryType()) == wkbPolygon) { OGRPolygon *p = (OGRPolygon *)g; OGRLinearRing *r = p->getExteriorRing(); if (!r) { fprintf(stderr, "Quad has no exterior polygon ring %s\n", id.toLatin1().data()); continue; } int size = r->getNumPoints(); QPolygonF boundary; for (int i = 0; i < size; i++) { OGRPoint p; r->getPoint(i, &p); boundary << QPointF(p.getX(), p.getY()); } QPolygonF projBoundary = pj->transformFrom(&pjIndex, boundary); quads[id] = Quad(series, id, name, projBoundary); } else { fprintf(stderr, "Missing or invalid geometry for quad %s\n", id.toLatin1().data()); } } OGRDataSource::DestroyDataSource(ds); }
MultiPolygonView::MultiPolygonView(MapMultiPolygon* mapMultiPolygon,bool polygonFill,osg::Vec4 color) { this->geometryMultiPolygons=new osg::Geometry; this->geometryEdgeMultiPolygons=new osg::Geometry; this->vertices=new osg::Vec3Array; osg::ref_ptr<osg::Vec3Array> verticesCopy=new osg::Vec3Array; this->geometryEdgeMultiPolygons->setVertexArray(this->vertices); this->geometryMultiPolygons->setVertexArray(verticesCopy); normals=new osg::Vec3Array; normals->push_back(osg::Vec3(0.0f,-1.0f,0.0f)); this->polygonFillColor=new osg::Vec4Array; this->polygonEdgeColor=new osg::Vec4Array; this->polygonFillColor->push_back(color); this->polygonEdgeColor->push_back(edgeColor); for (int i=0;i<mapMultiPolygon->getNumGeometries();i++) { OGRGeometry* ogrGeom=mapMultiPolygon->getGeometryRef(i); OGRwkbGeometryType ogrGeomType=ogrGeom->getGeometryType(); if (wkbPolygon != ogrGeomType && wkbPolygon25D != ogrGeomType) continue; // skip MapPolygon* mapPolygon=static_cast< MapPolygon*>(ogrGeom); OGRLinearRing *poExteriorRing=mapPolygon->getExteriorRing(); OGRPoint point; for(int i = 0; i < poExteriorRing->getNumPoints(); i++) { poExteriorRing->getPoint(i, &point); vertices->push_back(osg::Vec3(point.getX(), 0, point.getY())); verticesCopy->push_back(osg::Vec3(point.getX(), 0, point.getY())); } this->geometryEdgeMultiPolygons->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_LOOP, vertices->size()-poExteriorRing->getNumPoints(), poExteriorRing->getNumPoints())); this->geometryMultiPolygons->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_LOOP, verticesCopy->size()-poExteriorRing->getNumPoints(), poExteriorRing->getNumPoints())); if (mapPolygon->getNumInteriorRings()) { for (int i = 0; i < mapPolygon->getNumInteriorRings(); i++) { OGRLinearRing *ring = mapPolygon->getInteriorRing(i); OGRPoint point; for (int j = 0; j < ring->getNumPoints(); j++) { ring->getPoint(j, &point); vertices->push_back(osg::Vec3(point.getX(), 0, point.getY())); verticesCopy->push_back(osg::Vec3(point.getX(), 0, point.getY())); } this->geometryEdgeMultiPolygons->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_LOOP, vertices->size()-ring->getNumPoints() , ring->getNumPoints())); this->geometryMultiPolygons->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_LOOP, verticesCopy->size()-ring->getNumPoints() , ring->getNumPoints())); } } } this->geometryEdgeMultiPolygons->setNormalArray(normals); this->geometryEdgeMultiPolygons->setNormalBinding(osg::Geometry::BIND_OVERALL); this->geometryEdgeMultiPolygons->setColorArray(this->polygonEdgeColor); this->geometryEdgeMultiPolygons->setColorBinding(osg::Geometry::BIND_OVERALL); /*if(polygonFill==true) {*/ osgUtil::Tessellator tsl; tsl.setTessellationType(osgUtil::Tessellator::TESS_TYPE_GEOMETRY); tsl.setBoundaryOnly(false); tsl.retessellatePolygons(*this->geometryMultiPolygons); osg::Vec3Array* array = triangulizeGeometry(this->geometryMultiPolygons); this->geometryMultiPolygons->setVertexArray(array); this->geometryMultiPolygons->removePrimitiveSet(0,this->geometryMultiPolygons->getNumPrimitiveSets()); this->geometryMultiPolygons->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::TRIANGLES, 0, array->size())); this->geometryMultiPolygons->setNormalArray(normals); this->geometryMultiPolygons->setNormalBinding(osg::Geometry::BIND_OVERALL); this->geometryMultiPolygons->setColorArray(this->polygonFillColor); this->geometryMultiPolygons->setColorBinding(osg::Geometry::BIND_OVERALL); //} }
void SavePolygons( const std::vector< std::string > InFilenames, const char *OutFilename, const cv::Mat klabels, const std::vector< cv::Mat > raster, const std::vector< u_int32_t > labelpixels, const std::vector< std::vector <double> > sumCH, const std::vector< std::vector <double> > avgCH, const std::vector< std::vector <double> > stdCH, std::vector< std::vector< LINE > >& linelists ) { CPLLocaleC oLocaleCForcer(); CPLErrorReset(); const char *pszDriverName = "ESRI Shapefile"; GDALDriver *liDriver; liDriver = GetGDALDriverManager()->GetDriverByName(pszDriverName ); if( liDriver == NULL ) { printf( "\nERROR: %s driver not available.\n", pszDriverName ); exit( 1 ); } const size_t m_bands = raster.size(); const size_t m_labels = labelpixels.size(); GDALDataset *liDS; liDS = liDriver->Create( OutFilename, 0, 0, 0, GDT_Unknown, NULL ); if( liDS == NULL ) { printf( "\nERROR: Creation of output file failed.\n" ); exit( 1 ); } // dataset GDALDataset* piDataset; piDataset = (GDALDataset*) GDALOpen(InFilenames[0].c_str(), GA_ReadOnly); // spatialref OGRSpatialReference oSRS; oSRS.SetProjCS( piDataset->GetProjectionRef() ); OGRLayer *liLayer; liLayer = liDS->CreateLayer( "segments", &oSRS, wkbPolygon, NULL ); if( liLayer == NULL ) { printf( "\nERROR: Layer creation failed.\n" ); exit( 1 ); } // spatial transform double adfGeoTransform[6]; double oX = 0.0f; double oY = 0.0f; double mX = 1.0f; double mY = -1.0f; if( piDataset->GetGeoTransform( adfGeoTransform ) == CE_None ) { oX = adfGeoTransform[0]; oY = adfGeoTransform[3]; mX = adfGeoTransform[1]; mY = adfGeoTransform[5]; } GDALClose( (GDALDatasetH) piDataset ); OGRFieldDefn *clsIdField = new OGRFieldDefn( "CLASS", OFTInteger ); liLayer->CreateField( clsIdField ); OGRFieldDefn *pixArField = new OGRFieldDefn( "AREA", OFTInteger ); liLayer->CreateField( pixArField ); for ( size_t b = 0; b < m_bands; b++ ) { stringstream value; value << b+1; std::string FieldName = value.str() + "_AVERAGE"; OGRFieldDefn *lavrgField = new OGRFieldDefn( FieldName.c_str(), OFTReal ); liLayer->CreateField( lavrgField ); } for ( size_t b = 0; b < m_bands; b++ ) { stringstream value; value << b+1; std::string FieldName = value.str() + "_STDDEV"; OGRFieldDefn *lavrgField = new OGRFieldDefn( FieldName.c_str(), OFTReal ); liLayer->CreateField( lavrgField ); } int multiring = 0; printf ("Write File: %s (polygon)\n", OutFilename); for (size_t k = 0; k < m_labels; k++) { if (multiring == 1) { k = k - 1; multiring = 0; } if (linelists[k].size() == 0) continue; // insert field data OGRFeature *liFeature; liFeature = OGRFeature::CreateFeature( liLayer->GetLayerDefn() ); liFeature->SetField( "CLASS", (int) k ); liFeature->SetField( "AREA", (int) labelpixels.at(k) ); for ( size_t b = 0; b < m_bands; b++ ) { stringstream value; value << b+1; std::string FieldName = value.str() + "_AVERAGE"; liFeature->SetField( FieldName.c_str(), (double) avgCH[b].at(k) ); } for ( size_t b = 0; b < m_bands; b++ ) { stringstream value; value << b+1; std::string FieldName = value.str() + "_STDDEV"; liFeature->SetField( FieldName.c_str(), stdCH[b].at(k) ); } // initiate polygon start OGRLinearRing linestring; linestring.setCoordinateDimension(2); linestring.addPoint( oX + (double) linelists[k][0].sX * mX, oY + mY * (double) linelists[k][0].sY ); linestring.addPoint( oX + (double) linelists[k][0].eX * mX, oY + mY * (double) linelists[k][0].eY ); linelists[k].erase( linelists[k].begin() ); // construct polygon from lines while ( linelists[k].size() > 0 ) { if (multiring == 1) break; vector<LINE>::iterator it = linelists[k].begin(); for (; it != linelists[k].end(); ++it) { double ltX = linestring.getX(linestring.getNumPoints()-1); double ltY = linestring.getY(linestring.getNumPoints()-1); double csX = oX + (double) it->sX * mX; double csY = oY + mY * (double) it->sY; double ceX = oX + (double) it->eX * mX; double ceY = oY + mY * (double) it->eY; if ( ( csX == ltX ) && ( csY == ltY ) ) { linestring.addPoint(ceX, ceY); linelists[k].erase(it); break; } if ( ( ceX == ltX ) && ( ceY == ltY ) ) { linestring.addPoint(csX, csY); linelists[k].erase(it); break; } if (it == linelists[k].end()-1) { multiring = 1; break; } } } OGRPolygon polygon; linestring.closeRings(); // simplify poligons // remove colinear vertices OGRLinearRing linesimple; float pointPrevX = 0, pointPrevY = 0; for (int i = 0; i < linestring.getNumPoints(); i++) { OGRPoint point; linestring.getPoint(i, &point); // start if ( i == 0) { linesimple.addPoint( &point ); pointPrevX = point.getX(); pointPrevY = point.getY(); continue; } // end vertex if ( i == linestring.getNumPoints() - 1 ) { linesimple.addPoint( &point ); continue; } OGRPoint pointNext; linestring.getPoint(i+1, &pointNext); // | x1 y1 1 | // det | x2 y2 1 | = 0 => p1,p2,p3 are colinear // | x3 y3 1 | // x1*(y2-y3) + x2*(y3-y1) + x3*(y1-y2) == 0 // only if not colinear with previous and next if ( pointPrevX*(point.getY()-pointNext.getY()) + point.getX()*(pointNext.getY()-pointPrevY) + pointNext.getX()*(pointPrevY-point.getY()) != 0 ) { linesimple.addPoint( &point ); pointPrevX = point.getX(); pointPrevY = point.getY(); } } // as polygon geometry polygon.addRing( &linesimple ); liFeature->SetGeometry( &polygon ); if( liLayer->CreateFeature( liFeature ) != OGRERR_NONE ) { printf( "\nERROR: Failed to create feature in shapefile.\n" ); exit( 1 ); } OGRFeature::DestroyFeature( liFeature ); GDALTermProgress( (float)(k+1) / (float)(m_labels), NULL, NULL ); } GDALTermProgress( 1.0f, NULL, NULL ); GDALClose( liDS ); }
bool transformVectorLayerToKmlGeometry(KmlLayerInfo<T>& LayerInfo) { // TODO manage when data are coming from datastore GDALDataset_COMPAT* DataSource; OGRLayer *Layer; OGRFeature *Feature; DataSource = GDALOpenRO_COMPAT(LayerInfo.SourceFilename.c_str()); if( DataSource == nullptr ) { OPENFLUID_LogWarning("Cannot open shapefile "+LayerInfo.SourceFilename+". This Kml output is ignored."); return false; } std::string LayerName = openfluid::tools::Filesystem::basename(LayerInfo.SourceFilename); Layer = DataSource->GetLayerByName(LayerName.c_str()); if (Layer == nullptr) { OPENFLUID_LogWarning("Cannot open shapefile layer from " + LayerInfo.SourceFilename + ". This Kml output is ignored."); return false; } int OfldIDFieldIndex = Layer->GetLayerDefn()->GetFieldIndex("OFLD_ID"); if (OfldIDFieldIndex < 0) { OPENFLUID_LogWarning("Cannot find OFLD_ID attribute in " + LayerInfo.SourceFilename + ". This Kml output is ignored."); return false; } Layer->ResetReading(); while((Feature = Layer->GetNextFeature()) != nullptr) { openfluid::core::UnitID_t UnitID = Feature->GetFieldAsInteger(OfldIDFieldIndex); if (OPENFLUID_IsUnitExist(LayerInfo.UnitsClass,UnitID)) { OGRGeometry *Geometry; Geometry = Feature->GetGeometryRef(); std::stringstream CoordSS; CoordSS << std::fixed << std::setprecision(12); T KUI; KUI.UnitID = UnitID; if (Geometry != nullptr) { if (Geometry->getGeometryType() != wkbPolygon && Geometry->getGeometryType() != wkbLineString) { OPENFLUID_LogWarning("Unsupported geometry type in " + LayerInfo.SourceFilename + ". This Kml output is ignored."); return false; } KUI.GeometryType = Geometry->getGeometryType(); // polygons if (Geometry->getGeometryType() == wkbPolygon) { OGRLinearRing* Ring; Ring = ((OGRPolygon*)(Geometry))->getExteriorRing(); int NumPoints = Ring->getNumPoints()-1; for (int i=0;i<NumPoints;i++) { OGRPoint *Point = new OGRPoint(); Ring->getPoint(i,Point); CoordSS << " " << Point->getX() << "," << Point->getY(); delete Point; } // close the polygon if (NumPoints > 0) { OGRPoint *Point = new OGRPoint(); Ring->getPoint(0,Point); CoordSS << " " << Point->getX() << "," << Point->getY(); delete Point; } } // line strings if (Geometry->getGeometryType() == wkbLineString) { OGRLineString* LineString; LineString = (OGRLineString*)(Geometry); int NumPoints = LineString->getNumPoints(); for (int i=0;i<NumPoints;i++) { OGRPoint *Point = new OGRPoint(); LineString->getPoint(i,Point); CoordSS << " " << Point->getX() << "," << Point->getY(); delete Point; } } KUI.CoordsStr = CoordSS.str(); LayerInfo.UnitsInfos[UnitID] = KUI; } else { OPENFLUID_LogWarning("Wrong geometry reference in " + LayerInfo.SourceFilename + ". This Kml output is ignored."); return false; } } OGRFeature::DestroyFeature(Feature); } GDALClose_COMPAT(DataSource); return true; }