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) {
118 std::vector<Parameterised*> parCont;
120 std::string
id = useRunningID ?
toString(runningID) : poFeature->GetFieldAsString(idField.c_str());
124 throw ProcessError(
"Missing id under '" + idField +
"'");
128 OGRGeometry* poGeometry = poFeature->GetGeometryRef();
129 if (poGeometry == 0) {
130 OGRFeature::DestroyFeature(poFeature);
134 poGeometry->transform(poCT);
135 OGRwkbGeometryType gtype = poGeometry->getGeometryType();
138 OGRPoint* cgeom = (OGRPoint*) poGeometry;
141 WRITE_ERROR(
"Unable to project coordinates for POI '" +
id +
"'.");
144 if (toFill.
insert(
id, poi, layer)) {
145 parCont.push_back(poi);
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 parCont.push_back(poly);
166 OGRLinearRing* cgeom = ((OGRPolygon*) poGeometry)->getExteriorRing();
168 for (
int j = 0; j < cgeom->getNumPoints(); j++) {
171 WRITE_ERROR(
"Unable to project coordinates for polygon '" +
id +
"'.");
176 if (toFill.
insert(
id, poly, layer)) {
177 parCont.push_back(poly);
181 case wkbMultiPoint: {
182 OGRMultiPoint* cgeom = (OGRMultiPoint*) poGeometry;
183 for (
int i = 0; i < cgeom->getNumGeometries(); ++i) {
184 OGRPoint* cgeom2 = (OGRPoint*) cgeom->getGeometryRef(i);
186 std::string tid =
id +
"#" +
toString(i);
188 WRITE_ERROR(
"Unable to project coordinates for POI '" + tid +
"'.");
191 if (toFill.
insert(tid, poi, layer)) {
192 parCont.push_back(poi);
197 case wkbMultiLineString: {
198 OGRMultiLineString* cgeom = (OGRMultiLineString*) poGeometry;
199 for (
int i = 0; i < cgeom->getNumGeometries(); ++i) {
200 OGRLineString* cgeom2 = (OGRLineString*) cgeom->getGeometryRef(i);
202 std::string tid =
id +
"#" +
toString(i);
203 for (
int j = 0; j < cgeom2->getNumPoints(); j++) {
206 WRITE_ERROR(
"Unable to project coordinates for polygon '" + tid +
"'.");
211 if (toFill.
insert(tid, poly, layer)) {
212 parCont.push_back(poly);
217 case wkbMultiPolygon: {
218 OGRMultiPolygon* cgeom = (OGRMultiPolygon*) poGeometry;
219 for (
int i = 0; i < cgeom->getNumGeometries(); ++i) {
220 OGRLinearRing* cgeom2 = ((OGRPolygon*) cgeom->getGeometryRef(i))->getExteriorRing();
222 std::string tid =
id +
"#" +
toString(i);
223 for (
int j = 0; j < cgeom2->getNumPoints(); j++) {
226 WRITE_ERROR(
"Unable to project coordinates for polygon '" + tid +
"'.");
231 if (toFill.
insert(tid, poly, layer)) {
232 parCont.push_back(poly);
238 WRITE_WARNING(
"Unsupported shape type occured (id='" +
id +
"').");
241 if (oc.
getBool(
"shapefile.add-param")) {
242 for (std::vector<Parameterised*>::const_iterator it = parCont.begin(); it != parCont.end(); ++it) {
243 OGRFeatureDefn* poFDefn = poLayer->GetLayerDefn();
244 for (
int iField = 0; iField < poFDefn->GetFieldCount(); iField++) {
245 OGRFieldDefn* poFieldDefn = poFDefn->GetFieldDefn(iField);
246 if (poFieldDefn->GetNameRef() != idField) {
247 if (poFieldDefn->GetType() == OFTReal) {
248 (*it)->addParameter(poFieldDefn->GetNameRef(),
toString(poFeature->GetFieldAsDouble(iField)));
256 OGRFeature::DestroyFeature(poFeature);
260 WRITE_ERROR(
"SUMO was compiled without GDAL support.");