void HostipRunner::slotRequestFinished( QNetworkReply* reply ) { double lon(0.0), lat(0.0); for ( QString line = reply->readLine(); !line.isEmpty(); line = reply->readLine() ) { QString lonInd = "Longitude: "; if ( line.startsWith(lonInd) ) { lon = line.mid( lonInd.length() ).toDouble(); } QString latInd = "Latitude: "; if (line.startsWith( latInd) ) { lat = line.mid( latInd.length() ).toDouble(); } } QVector<GeoDataPlacemark*> placemarks; if (lon != 0.0 && lat != 0.0) { GeoDataPlacemark *placemark = new GeoDataPlacemark; placemark->setName( m_hostInfo.hostName() ); QString description("%1 (%2)"); placemark->setDescription( description. arg( m_hostInfo.hostName() ). arg( m_hostInfo.addresses().first().toString() ) ); placemark->setCoordinate( lon * DEG2RAD, lat * DEG2RAD ); placemark->setVisualCategory( GeoDataFeature::Coordinate ); placemarks << placemark; } emit searchFinished( placemarks ); }
void OsmNominatimRunner::handleResult( QNetworkReply* reply ) { QDomDocument xml; if (!xml.setContent(reply->readAll())) { qWarning() << "Cannot parse osm nominatim result"; returnNoResults(); return; } QVector<GeoDataPlacemark*> placemarks; QDomElement root = xml.documentElement(); QDomNodeList places = root.elementsByTagName("place"); for (int i=0; i<places.size(); ++i) { QDomNode place = places.at(i); QDomNamedNodeMap attributes = place.attributes(); QString lon = attributes.namedItem("lon").nodeValue(); QString lat = attributes.namedItem("lat").nodeValue(); QString desc = attributes.namedItem("display_name").nodeValue(); QString key = attributes.namedItem("class").nodeValue(); QString value = attributes.namedItem("type").nodeValue(); QString name = place.firstChildElement(value).text(); QString road = place.firstChildElement("road").text(); QString city = place.firstChildElement("city").text(); if( city.isEmpty() ) { city = place.firstChildElement("town").text(); if( city.isEmpty() ) { city = place.firstChildElement("village").text(); } if( city.isEmpty() ) { city = place.firstChildElement("hamlet").text(); } } QString administrative = place.firstChildElement("county").text(); if( administrative.isEmpty() ) { administrative = place.firstChildElement("region").text(); if( administrative.isEmpty() ) { administrative = place.firstChildElement("state").text(); } } QString country = place.firstChildElement("country").text(); QString description; for (int i=0; i<place.childNodes().size(); ++i) { QDomElement item = place.childNodes().at(i).toElement(); description += item.nodeName() + ':' + item.text() + '\n'; } description += "Category: " + key + '/' + value; if (!lon.isEmpty() && !lat.isEmpty() && !desc.isEmpty()) { QString placemarkName; GeoDataPlacemark* placemark = new GeoDataPlacemark; // try to provide 2 fields if (!name.isEmpty()) { placemarkName = name; } if (!road.isEmpty() && road != placemarkName ) { if( !placemarkName.isEmpty() ) { placemarkName += ", "; } placemarkName += road; } if (!city.isEmpty() && !placemarkName.contains(",") && city != placemarkName) { if( !placemarkName.isEmpty() ) { placemarkName += ", "; } placemarkName += city; } if (!administrative.isEmpty()&& !placemarkName.contains(",") && administrative != placemarkName) { if( !placemarkName.isEmpty() ) { placemarkName += ", "; } placemarkName += administrative; } if (!country.isEmpty()&& !placemarkName.contains(",") && country != placemarkName) { if( !placemarkName.isEmpty() ) { placemarkName += ", "; } placemarkName += country; } if (placemarkName.isEmpty()) { placemarkName = desc; } placemark->setName( placemarkName ); placemark->setDescription(description); placemark->setCoordinate( lon.toDouble(), lat.toDouble(), 0, GeoDataCoordinates::Degree ); GeoDataFeature::GeoDataVisualCategory category = GeoDataFeature::OsmVisualCategory( key + '=' + value ); placemark->setVisualCategory( category ); placemarks << placemark; } } emit searchFinished( placemarks ); }
GeoDataDocument *ShpRunner::parseFile(const QString &fileName, DocumentRole role, QString &error) { QFileInfo fileinfo( fileName ); if (fileinfo.suffix().compare(QLatin1String("shp"), Qt::CaseInsensitive) != 0) { error = QStringLiteral("File %1 does not have a shp suffix").arg(fileName); mDebug() << error; return nullptr; } SHPHandle handle = SHPOpen( fileName.toStdString().c_str(), "rb" ); if ( !handle ) { error = QStringLiteral("Failed to read %1").arg(fileName); mDebug() << error; return nullptr; } int entities; int shapeType; SHPGetInfo( handle, &entities, &shapeType, NULL, NULL ); mDebug() << " SHP info " << entities << " Entities " << shapeType << " Shape Type "; DBFHandle dbfhandle; dbfhandle = DBFOpen( fileName.toStdString().c_str(), "rb"); int nameField = DBFGetFieldIndex( dbfhandle, "Name" ); int noteField = DBFGetFieldIndex( dbfhandle, "Note" ); int mapColorField = DBFGetFieldIndex( dbfhandle, "mapcolor13" ); GeoDataDocument *document = new GeoDataDocument; document->setDocumentRole( role ); if ( mapColorField != -1 ) { GeoDataSchema schema; schema.setId(QStringLiteral("default")); GeoDataSimpleField simpleField; simpleField.setName(QStringLiteral("mapcolor13")); simpleField.setType( GeoDataSimpleField::Double ); schema.addSimpleField( simpleField ); document->addSchema( schema ); } for ( int i=0; i< entities; ++i ) { GeoDataPlacemark *placemark = 0; placemark = new GeoDataPlacemark; document->append( placemark ); SHPObject *shape = SHPReadObject( handle, i ); if (nameField != -1) { const char* info = DBFReadStringAttribute( dbfhandle, i, nameField ); // TODO: defaults to utf-8 encoding, but could be also something else, optionally noted in a .cpg file placemark->setName( info ); mDebug() << "name " << placemark->name(); } if (noteField != -1) { const char* note = DBFReadStringAttribute( dbfhandle, i, noteField ); // TODO: defaults to utf-8 encoding, see comment for name placemark->setDescription( note ); mDebug() << "desc " << placemark->description(); } double mapColor = DBFReadDoubleAttribute( dbfhandle, i, mapColorField ); if ( mapColor ) { GeoDataStyle::Ptr style(new GeoDataStyle); if ( mapColor >= 0 && mapColor <=255 ) { quint8 colorIndex = quint8( mapColor ); style->polyStyle().setColorIndex( colorIndex ); } else { quint8 colorIndex = 0; // mapColor is undefined in this case style->polyStyle().setColorIndex( colorIndex ); } placemark->setStyle( style ); } switch ( shapeType ) { case SHPT_POINT: { GeoDataPoint *point = new GeoDataPoint( *shape->padfX, *shape->padfY, 0, GeoDataCoordinates::Degree ); placemark->setGeometry( point ); mDebug() << "point " << placemark->name(); break; } case SHPT_MULTIPOINT: { GeoDataMultiGeometry *geom = new GeoDataMultiGeometry; for( int j=0; j<shape->nVertices; ++j ) { geom->append( new GeoDataPoint( GeoDataCoordinates( shape->padfX[j], shape->padfY[j], 0, GeoDataCoordinates::Degree ) ) ); } placemark->setGeometry( geom ); mDebug() << "multipoint " << placemark->name(); break; } case SHPT_ARC: { if ( shape->nParts != 1 ) { GeoDataMultiGeometry *geom = new GeoDataMultiGeometry; for( int j=0; j<shape->nParts; ++j ) { GeoDataLineString *line = new GeoDataLineString; int itEnd = (j + 1 < shape->nParts) ? shape->panPartStart[j+1] : shape->nVertices; for( int k=shape->panPartStart[j]; k<itEnd; ++k ) { line->append( GeoDataCoordinates( shape->padfX[k], shape->padfY[k], 0, GeoDataCoordinates::Degree ) ); } geom->append( line ); } placemark->setGeometry( geom ); mDebug() << "arc " << placemark->name() << " " << shape->nParts; } else { GeoDataLineString *line = new GeoDataLineString; for( int j=0; j<shape->nVertices; ++j ) { line->append( GeoDataCoordinates( shape->padfX[j], shape->padfY[j], 0, GeoDataCoordinates::Degree ) ); } placemark->setGeometry( line ); mDebug() << "arc " << placemark->name() << " " << shape->nParts; } break; } case SHPT_POLYGON: { if ( shape->nParts != 1 ) { bool isRingClockwise = false; GeoDataMultiGeometry *multigeom = new GeoDataMultiGeometry; GeoDataPolygon *poly = 0; int polygonCount = 0; for( int j=0; j<shape->nParts; ++j ) { GeoDataLinearRing ring; int itStart = shape->panPartStart[j]; int itEnd = (j + 1 < shape->nParts) ? shape->panPartStart[j+1] : shape->nVertices; for( int k = itStart; k<itEnd; ++k ) { ring.append( GeoDataCoordinates( shape->padfX[k], shape->padfY[k], 0, GeoDataCoordinates::Degree ) ); } isRingClockwise = ring.isClockwise(); if ( j == 0 || isRingClockwise ) { poly = new GeoDataPolygon; ++polygonCount; poly->setOuterBoundary( ring ); if ( polygonCount > 1 ) { multigeom->append( poly ); } } else { poly->appendInnerBoundary( ring ); } } if ( polygonCount > 1 ) { placemark->setGeometry( multigeom ); } else { placemark->setGeometry( poly ); delete multigeom; multigeom = 0; } mDebug() << "donut " << placemark->name() << " " << shape->nParts; } else { GeoDataPolygon *poly = new GeoDataPolygon; GeoDataLinearRing ring; for( int j=0; j<shape->nVertices; ++j ) { ring.append( GeoDataCoordinates( shape->padfX[j], shape->padfY[j], 0, GeoDataCoordinates::Degree ) ); } poly->setOuterBoundary( ring ); placemark->setGeometry( poly ); mDebug() << "poly " << placemark->name() << " " << shape->nParts; } break; } } } SHPClose( handle ); DBFClose( dbfhandle ); if ( document->size() ) { document->setFileName( fileName ); return document; } else { delete document; return nullptr; } }
void OsmNominatimRunner::handleResult( QNetworkReply* reply ) { QDomDocument xml; if (!xml.setContent(reply->readAll())) { qWarning() << "Cannot parse osm nominatim result"; qWarning() << reply->error(); returnNoResults(); return; } QVector<GeoDataPlacemark*> placemarks; QDomElement root = xml.documentElement(); QDomNodeList places = root.elementsByTagName(QStringLiteral("place")); for (int i=0; i<places.size(); ++i) { QDomNode place = places.at(i); QDomNamedNodeMap attributes = place.attributes(); QString lon = attributes.namedItem(QStringLiteral("lon")).nodeValue(); QString lat = attributes.namedItem(QStringLiteral("lat")).nodeValue(); QString desc = attributes.namedItem(QStringLiteral("display_name")).nodeValue(); QString key = attributes.namedItem(QStringLiteral("class")).nodeValue(); QString value = attributes.namedItem(QStringLiteral("type")).nodeValue(); OsmPlacemarkData data; GeoDataExtendedData placemarkData = extractChildren(place); placemarkData.addValue(GeoDataData(QStringLiteral("class"), key)); placemarkData.addValue(GeoDataData(QStringLiteral("type"), value)); QString name = place.firstChildElement(value).text(); QString road = place.firstChildElement(QStringLiteral("road")).text(); placemarkData.addValue(GeoDataData(QStringLiteral("name"), name)); QString city = place.firstChildElement(QStringLiteral("city")).text(); if( city.isEmpty() ) { city = place.firstChildElement(QStringLiteral("town")).text(); if( city.isEmpty() ) { city = place.firstChildElement(QStringLiteral("village")).text(); } if( city.isEmpty() ) { city = place.firstChildElement(QStringLiteral("hamlet")).text(); } } QString administrative = place.firstChildElement(QStringLiteral("county")).text(); if( administrative.isEmpty() ) { administrative = place.firstChildElement(QStringLiteral("region")).text(); if( administrative.isEmpty() ) { administrative = place.firstChildElement(QStringLiteral("state")).text(); data.addTag(QStringLiteral("addr:state"), administrative); } else { data.addTag(QStringLiteral("district"), administrative); } } QString country = place.firstChildElement(QStringLiteral("country")).text(); QString description; for (int i=0; i<place.childNodes().size(); ++i) { QDomElement item = place.childNodes().at(i).toElement(); description += item.nodeName() + QLatin1Char(':') + item.text() + QLatin1Char('\n'); } description += QLatin1String("Category: ") + key + QLatin1Char('/') + value; if (!lon.isEmpty() && !lat.isEmpty() && !desc.isEmpty()) { QString placemarkName; GeoDataPlacemark* placemark = new GeoDataPlacemark; // try to provide 2 fields if (!name.isEmpty()) { placemarkName = name; } if (!road.isEmpty() && road != placemarkName ) { if( !placemarkName.isEmpty() ) { placemarkName += QLatin1String(", "); } placemarkName += road; data.addTag(QStringLiteral("addr:street"), road); } if (!city.isEmpty() && !placemarkName.contains(QLatin1Char(',')) && city != placemarkName) { if( !placemarkName.isEmpty() ) { placemarkName += QLatin1String(", "); } placemarkName += city; data.addTag(QStringLiteral("addr:city"), city); } if (!administrative.isEmpty() && !placemarkName.contains(QLatin1Char(',')) && administrative != placemarkName) { if( !placemarkName.isEmpty() ) { placemarkName += QLatin1String(", "); } placemarkName += administrative; } if (!country.isEmpty() && !placemarkName.contains(QLatin1Char(',')) && country != placemarkName) { if( !placemarkName.isEmpty() ) { placemarkName += QLatin1String(", "); } placemarkName += country; data.addTag(QStringLiteral("addr:country"), country); } if (placemarkName.isEmpty()) { placemarkName = desc; } placemark->setName( placemarkName ); placemark->setDescription(description); placemark->setAddress(desc); placemark->setCoordinate( lon.toDouble(), lat.toDouble(), 0, GeoDataCoordinates::Degree ); const auto category = StyleBuilder::determineVisualCategory(data); placemark->setVisualCategory( category ); placemark->setExtendedData(placemarkData); placemark->setOsmData(data); placemarks << placemark; } } emit searchFinished( placemarks ); }