void OsmNominatimRunner::startSearch() { QNetworkReply *reply = m_manager.get( m_request ); connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(returnNoResults())); }
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 ); }
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 ); }