45 #include <ogrsf_frmts.h>
48 #ifdef CHECK_MEMORY_LEAKS
50 #endif // CHECK_MEMORY_LEAKS
59 if (!oc.
isSet(
"shapefile-prefixes")) {
63 std::vector<std::string> files = oc.
getStringVector(
"shapefile-prefixes");
64 for (std::vector<std::string>::const_iterator file = files.begin(); file != files.end(); ++file) {
66 load(*file, oc, toFill, tm);
79 std::string prefix = oc.
getString(
"prefix");
82 int layer = oc.
getInt(
"layer");
83 std::string idField = oc.
getString(
"shapefile.id-column");
84 bool useRunningID = oc.
getBool(
"shapefile.use-running-id");
86 std::string shpName = file +
".shp";
88 OGRDataSource* poDS = OGRSFDriverRegistrar::Open(shpName.c_str(), FALSE);
90 throw ProcessError(
"Could not open shape description '" + shpName +
"'.");
94 OGRLayer* poLayer = poDS->GetLayer(0);
95 poLayer->ResetReading();
98 OGRSpatialReference* origTransf = poLayer->GetSpatialRef();
99 OGRSpatialReference destTransf;
101 destTransf.SetWellKnownGeogCS(
"WGS84");
102 OGRCoordinateTransformation* poCT = OGRCreateCoordinateTransformation(origTransf, &destTransf);
104 if (oc.
isSet(
"shapefile.guess-projection")) {
105 OGRSpatialReference origTransf2;
106 origTransf2.SetWellKnownGeogCS(
"WGS84");
107 poCT = OGRCreateCoordinateTransformation(&origTransf2, &destTransf);
110 WRITE_WARNING(
"Could not create geocoordinates converter; check whether proj.4 is installed.");
114 OGRFeature* poFeature;
115 poLayer->ResetReading();
116 unsigned int runningID = 0;
117 while ((poFeature = poLayer->GetNextFeature()) != NULL) {
119 std::string
id = useRunningID ?
toString(runningID) : poFeature->GetFieldAsString(idField.c_str());
123 throw ProcessError(
"Missing id under '" + idField +
"'");
127 OGRGeometry* poGeometry = poFeature->GetGeometryRef();
128 if (poGeometry == 0) {
129 OGRFeature::DestroyFeature(poFeature);
133 poGeometry->transform(poCT);
134 OGRwkbGeometryType gtype = poGeometry->getGeometryType();
137 OGRPoint* cgeom = (OGRPoint*) poGeometry;
140 WRITE_ERROR(
"Unable to project coordinates for POI '" +
id +
"'.");
143 if (!toFill.
insert(
id, poi, layer)) {
144 WRITE_ERROR(
"POI '" +
id +
"' could not be added.");
149 case wkbLineString: {
150 OGRLineString* cgeom = (OGRLineString*) poGeometry;
152 for (
int j = 0; j < cgeom->getNumPoints(); j++) {
155 WRITE_ERROR(
"Unable to project coordinates for polygon '" +
id +
"'.");
160 if (!toFill.
insert(
id, poly, layer)) {
161 WRITE_ERROR(
"Polygon '" +
id +
"' could not be added.");
167 OGRLinearRing* cgeom = ((OGRPolygon*) poGeometry)->getExteriorRing();
169 for (
int j = 0; j < cgeom->getNumPoints(); j++) {
172 WRITE_ERROR(
"Unable to project coordinates for polygon '" +
id +
"'.");
177 if (!toFill.
insert(
id, poly, layer)) {
178 WRITE_ERROR(
"Polygon '" +
id +
"' could not be added.");
183 case wkbMultiPoint: {
184 OGRMultiPoint* cgeom = (OGRMultiPoint*) poGeometry;
185 for (
int i = 0; i < cgeom->getNumGeometries(); ++i) {
186 OGRPoint* cgeom2 = (OGRPoint*) cgeom->getGeometryRef(i);
188 std::string tid =
id +
"#" +
toString(i);
190 WRITE_ERROR(
"Unable to project coordinates for POI '" + tid +
"'.");
193 if (!toFill.
insert(tid, poi, layer)) {
194 WRITE_ERROR(
"POI '" + tid +
"' could not be added.");
200 case wkbMultiLineString: {
201 OGRMultiLineString* cgeom = (OGRMultiLineString*) poGeometry;
202 for (
int i = 0; i < cgeom->getNumGeometries(); ++i) {
203 OGRLineString* cgeom2 = (OGRLineString*) cgeom->getGeometryRef(i);
205 std::string tid =
id +
"#" +
toString(i);
206 for (
int j = 0; j < cgeom2->getNumPoints(); j++) {
209 WRITE_ERROR(
"Unable to project coordinates for polygon '" + tid +
"'.");
214 if (!toFill.
insert(tid, poly, layer)) {
215 WRITE_ERROR(
"Polygon '" + tid +
"' could not be added.");
221 case wkbMultiPolygon: {
222 OGRMultiPolygon* cgeom = (OGRMultiPolygon*) poGeometry;
223 for (
int i = 0; i < cgeom->getNumGeometries(); ++i) {
224 OGRLinearRing* cgeom2 = ((OGRPolygon*) cgeom->getGeometryRef(i))->getExteriorRing();
226 std::string tid =
id +
"#" +
toString(i);
227 for (
int j = 0; j < cgeom2->getNumPoints(); j++) {
230 WRITE_ERROR(
"Unable to project coordinates for polygon '" + tid +
"'.");
235 if (!toFill.
insert(tid, poly, layer)) {
236 WRITE_ERROR(
"Polygon '" + tid +
"' could not be added.");
243 WRITE_WARNING(
"Unsupported shape type occured (id='" +
id +
"').");
246 OGRFeature::DestroyFeature(poFeature);
250 WRITE_ERROR(
"SUMO was compiled without GDAL support.");