Skip to content

Instantly share code, notes, and snippets.

@benjbaron
Last active April 26, 2018 13:33
Show Gist options
  • Save benjbaron/f775adb7bf1334ddf483 to your computer and use it in GitHub Desktop.
Save benjbaron/f775adb7bf1334ddf483 to your computer and use it in GitHub Desktop.
Use OGR to parse a Shapefile (here, we want to parse an OSM Shapefile to get the main roads)
OGRLineString * convertFromGEOStoOGR(LineString * ls) {
OGRLineString * lineString = (OGRLineString*) OGRGeometryFactory::createGeometry(wkbLineString);
for(int i = 0; i < ls->getNumPoints(); ++i) {
Point * pt = ls->getPointN(i);
lineString->addPoint(pt->getX(), pt->getY());
}
return lineString;
}
using namespace geos;
using namespace geos::geom;
LineString * convertFromOGRToGEOS(OGRLineString * ls) {
CoordinateSequence * coordinates = new CoordinateArraySequence();
for(int i = 0; i < ls->getNumPoints(); ++i) {
OGRPoint pt;
ls->getPoint(i, &pt);
Coordinate coord(pt.getX(), pt.getY());
coordinates->add(coord);
}
GeometryFactory globalFactory;
return globalFactory.createLineString(coordinates);
}
Point * convertFromOGRToGEOS(OGRPoint * pt) {
GeometryFactory globalFactory;
return globalFactory.createPoint(Coordinate(pt->getX(), pt->getY()));
}
OGRRegisterAll();
OGRDataSource *poDS;
poDS = OGRSFDriverRegistrar::Open(filename.toStdString().c_str(), FALSE);
if( poDS == NULL )
{
qWarning() << "Open failed for " << filename;
return;
}
QList<QPair<OGRGeometry*,GeometryAttribute*> > _shapestoDraw;
QSet<QString> acceptedHWFieldsSet = QSet<QString>();
acceptedHWFieldsSet << "primary_link" << "tertiary_link" << "trunk_link" << "motorway" << "road" << "secondary_link" << "tertiary" << "motorway_link" << "secondary" << "trunk" << "primary";
for(int i = 0; i < poDS->GetLayerCount(); ++i) {
OGRLayer *poLayer = poDS->GetLayer(i);
qDebug() << "Loading layer" << QString::fromStdString(poLayer->GetName()) << "...";
OGRFeature *poFeature;
OGRSpatialReference *poTarget = new OGRSpatialReference();
poTarget->importFromProj4(_project->loader()->getOutputProj());
poLayer->ResetReading();
OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();
int hwIdx = poFDefn->GetFieldIndex("highway");
while( (poFeature = poLayer->GetNextFeature()) != NULL )
{
QString HWFieldStr = QString::fromStdString(poFeature->GetFieldAsString(hwIdx));
if(acceptedHWFieldsSet.contains(HWFieldStr)) {
OGRGeometry *poGeometry = poFeature->GetGeometryRef();
if( poGeometry != NULL
&& wkbFlatten(poGeometry->getGeometryType()) == wkbPoint )
{
poGeometry->transformTo(poTarget);
OGRPoint * pt = (OGRPoint *) poGeometry;
_shapestoDraw.append(qMakePair(pt, new GeometryAttribute(50,QColor(0,0,255))));
}
else if (poGeometry != NULL
&& wkbFlatten(poGeometry->getGeometryType()) == wkbLineString)
{
poGeometry->transformTo(poTarget);
OGRLineString * ls = (OGRLineString *) poGeometry;
_shapestoDraw.append(qMakePair(ls, new GeometryAttribute(1,QColor(255,0,0))));
}
}
}
}
// Do not delete any structure as they will be used later
qDebug() << "loaded shapefile with" << _shapestoDraw.count() << "features";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment