00001
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifdef _MSC_VER
00025 #include <windows_config.h>
00026 #else
00027 #include <config.h>
00028 #endif
00029
00030 #include <string>
00031 #include <utils/common/MsgHandler.h>
00032 #include <utils/common/ToString.h>
00033 #include <utils/common/StringUtils.h>
00034 #include <utils/options/OptionsCont.h>
00035 #include <utils/geom/GeomHelper.h>
00036 #include "PCLoaderArcView.h"
00037 #include <utils/geom/GeoConvHelper.h>
00038 #include <utils/common/RGBColor.h>
00039 #include <polyconvert/PCPolyContainer.h>
00040
00041 #ifdef HAVE_GDAL
00042 #include <ogrsf_frmts.h>
00043 #endif
00044
00045 #ifdef CHECK_MEMORY_LEAKS
00046 #include <foreign/nvwa/debug_new.h>
00047 #endif // CHECK_MEMORY_LEAKS
00048
00049
00050
00051
00052
00053 void
00054 PCLoaderArcView::loadIfSet(OptionsCont &oc, PCPolyContainer &toFill,
00055 PCTypeMap &tm) throw(ProcessError) {
00056 if (!oc.isSet("shape-files")) {
00057 return;
00058 }
00059
00060 std::vector<std::string> files = oc.getStringVector("shape-files");
00061 for (std::vector<std::string>::const_iterator file=files.begin(); file!=files.end(); ++file) {
00062 MsgHandler::getMessageInstance()->beginProcessMsg("Parsing from shape-file '" + *file + "'...");
00063 load(*file, oc, toFill, tm);
00064 MsgHandler::getMessageInstance()->endProcessMsg("done.");
00065 }
00066 }
00067
00068
00069
00070 void
00071 PCLoaderArcView::load(const std::string &file, OptionsCont &oc, PCPolyContainer &toFill,
00072 PCTypeMap &) throw(ProcessError) {
00073 #ifdef HAVE_GDAL
00074
00075 std::string prefix = oc.getString("prefix");
00076 std::string type = oc.getString("type");
00077 RGBColor color = RGBColor::parseColor(oc.getString("color"));
00078 int layer = oc.getInt("layer");
00079 std::string idField = oc.getString("shape-files.id-name");
00080
00081 std::string shpName = file + ".shp";
00082 OGRRegisterAll();
00083 OGRDataSource *poDS = OGRSFDriverRegistrar::Open(shpName.c_str(), FALSE);
00084 if (poDS == NULL) {
00085 throw ProcessError("Could not open shape description '" + shpName + "'.");
00086 }
00087
00088
00089 OGRLayer *poLayer = poDS->GetLayer(0);
00090 poLayer->ResetReading();
00091
00092
00093 OGRSpatialReference *origTransf = poLayer->GetSpatialRef();
00094 OGRSpatialReference destTransf;
00095
00096 destTransf.SetWellKnownGeogCS("WGS84");
00097 OGRCoordinateTransformation *poCT = OGRCreateCoordinateTransformation(origTransf, &destTransf);
00098 if (poCT == NULL) {
00099 if (oc.isSet("arcview.guess-projection")) {
00100 OGRSpatialReference origTransf2;
00101 origTransf2.SetWellKnownGeogCS("WGS84");
00102 poCT = OGRCreateCoordinateTransformation(&origTransf2, &destTransf);
00103 }
00104 if (poCT==0) {
00105 WRITE_WARNING("Could not create geocoordinates converter; check whether proj.4 is installed.");
00106 }
00107 }
00108
00109 OGRFeature *poFeature;
00110 poLayer->ResetReading();
00111 while ((poFeature = poLayer->GetNextFeature()) != NULL) {
00112
00113 std::string id = poFeature->GetFieldAsString(idField.c_str());
00114 id = StringUtils::prune(id);
00115 if (id=="") {
00116 throw ProcessError("Missing id under '" + idField + "'");
00117 }
00118 id = prefix + id;
00119
00120 OGRGeometry *poGeometry = poFeature->GetGeometryRef();
00121 if (poGeometry!=0) {
00122
00123 poGeometry->transform(poCT);
00124 }
00125 OGRwkbGeometryType gtype = poGeometry->getGeometryType();
00126 switch (gtype) {
00127 case wkbPoint: {
00128 OGRPoint *cgeom = (OGRPoint*) poGeometry;
00129 Position2D pos((SUMOReal) cgeom->getX(), (SUMOReal) cgeom->getY());
00130 if (!GeoConvHelper::x2cartesian(pos)) {
00131 MsgHandler::getErrorInstance()->inform("Unable to project coordinates for POI '" + id + "'.");
00132 }
00133 PointOfInterest *poi = new PointOfInterest(id, type, pos, color);
00134 if (!toFill.insert(id, poi, layer)) {
00135 MsgHandler::getErrorInstance()->inform("POI '" + id + "' could not been added.");
00136 delete poi;
00137 }
00138 }
00139 break;
00140 case wkbLineString: {
00141 OGRLineString *cgeom = (OGRLineString*) poGeometry;
00142 Position2DVector shape;
00143 for (int j=0; j<cgeom->getNumPoints(); j++) {
00144 Position2D pos((SUMOReal) cgeom->getX(j), (SUMOReal) cgeom->getY(j));
00145 if (!GeoConvHelper::x2cartesian(pos)) {
00146 MsgHandler::getErrorInstance()->inform("Unable to project coordinates for polygon '" + id + "'.");
00147 }
00148 shape.push_back_noDoublePos(pos);
00149 }
00150 Polygon2D *poly = new Polygon2D(id, type, color, shape, false);
00151 if (!toFill.insert(id, poly, layer)) {
00152 MsgHandler::getErrorInstance()->inform("Polygon '" + id + "' could not been added.");
00153 delete poly;
00154 }
00155 }
00156 break;
00157 case wkbPolygon: {
00158 OGRLinearRing *cgeom = ((OGRPolygon*) poGeometry)->getExteriorRing();
00159 Position2DVector shape;
00160 for (int j=0; j<cgeom->getNumPoints(); j++) {
00161 Position2D pos((SUMOReal) cgeom->getX(j), (SUMOReal) cgeom->getY(j));
00162 if (!GeoConvHelper::x2cartesian(pos)) {
00163 MsgHandler::getErrorInstance()->inform("Unable to project coordinates for polygon '" + id + "'.");
00164 }
00165 shape.push_back_noDoublePos(pos);
00166 }
00167 Polygon2D *poly = new Polygon2D(id, type, color, shape, true);
00168 if (!toFill.insert(id, poly, layer)) {
00169 MsgHandler::getErrorInstance()->inform("Polygon '" + id + "' could not been added.");
00170 delete poly;
00171 }
00172 }
00173 break;
00174 case wkbMultiPoint: {
00175 OGRMultiPoint *cgeom = (OGRMultiPoint*) poGeometry;
00176 for (int i=0; i<cgeom->getNumGeometries(); ++i) {
00177 OGRPoint *cgeom2 = (OGRPoint*) cgeom->getGeometryRef(i);
00178 Position2D pos((SUMOReal) cgeom2->getX(), (SUMOReal) cgeom2->getY());
00179 std::string tid = id + "#" + toString(i);
00180 if (!GeoConvHelper::x2cartesian(pos)) {
00181 MsgHandler::getErrorInstance()->inform("Unable to project coordinates for POI '" + tid + "'.");
00182 }
00183 PointOfInterest *poi = new PointOfInterest(tid, type, pos, color);
00184 if (!toFill.insert(tid, poi, layer)) {
00185 MsgHandler::getErrorInstance()->inform("POI '" + tid + "' could not been added.");
00186 delete poi;
00187 }
00188 }
00189 }
00190 break;
00191 case wkbMultiLineString: {
00192 OGRMultiLineString *cgeom = (OGRMultiLineString*) poGeometry;
00193 for (int i=0; i<cgeom->getNumGeometries(); ++i) {
00194 OGRLineString *cgeom2 = (OGRLineString*) cgeom->getGeometryRef(i);
00195 Position2DVector shape;
00196 std::string tid = id + "#" + toString(i);
00197 for (int j=0; j<cgeom2->getNumPoints(); j++) {
00198 Position2D pos((SUMOReal) cgeom2->getX(j), (SUMOReal) cgeom2->getY(j));
00199 if (!GeoConvHelper::x2cartesian(pos)) {
00200 MsgHandler::getErrorInstance()->inform("Unable to project coordinates for polygon '" + tid + "'.");
00201 }
00202 shape.push_back_noDoublePos(pos);
00203 }
00204 Polygon2D *poly = new Polygon2D(tid, type, color, shape, false);
00205 if (!toFill.insert(tid, poly, layer)) {
00206 MsgHandler::getErrorInstance()->inform("Polygon '" + tid + "' could not been added.");
00207 delete poly;
00208 }
00209 }
00210 }
00211 break;
00212 case wkbMultiPolygon: {
00213 OGRMultiPolygon *cgeom = (OGRMultiPolygon*) poGeometry;
00214 for (int i=0; i<cgeom->getNumGeometries(); ++i) {
00215 OGRLinearRing *cgeom2 = ((OGRPolygon*) cgeom->getGeometryRef(i))->getExteriorRing();
00216 Position2DVector shape;
00217 std::string tid = id + "#" + toString(i);
00218 for (int j=0; j<cgeom2->getNumPoints(); j++) {
00219 Position2D pos((SUMOReal) cgeom2->getX(j), (SUMOReal) cgeom2->getY(j));
00220 if (!GeoConvHelper::x2cartesian(pos)) {
00221 MsgHandler::getErrorInstance()->inform("Unable to project coordinates for polygon '" + tid + "'.");
00222 }
00223 shape.push_back_noDoublePos(pos);
00224 }
00225 Polygon2D *poly = new Polygon2D(tid, type, color, shape, true);
00226 if (!toFill.insert(tid, poly, layer)) {
00227 MsgHandler::getErrorInstance()->inform("Polygon '" + tid + "' could not been added.");
00228 delete poly;
00229 }
00230 }
00231 }
00232 break;
00233 default:
00234 MsgHandler::getWarningInstance()->inform("Unsupported shape type occured (id='" + id + "').");
00235 break;
00236 }
00237 OGRFeature::DestroyFeature(poFeature);
00238 }
00239 MsgHandler::getMessageInstance()->endProcessMsg("done.");
00240 #else
00241 MsgHandler::getErrorInstance()->inform("SUMO was compiled without GDAL support.");
00242 #endif
00243 }
00244
00245
00246
00247