[gnome-maps/wip/mlundblad/handle-osm-urls: 2/5] WIP: place: Add parsing of OSM URLs



commit b1de1e0f70c7b089e63dc751a75d5dfe1533249a
Author: Marcus Lundblad <ml update uu se>
Date:   Thu Jun 13 21:54:54 2019 +0200

    WIP: place: Add parsing of OSM URLs

 src/place.js | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 88 insertions(+)
---
diff --git a/src/place.js b/src/place.js
index 7d72af2..36e5b98 100644
--- a/src/place.js
+++ b/src/place.js
@@ -19,10 +19,14 @@
  * Author: Jonas Danielsson <jonas threetimestwo org>
  */
 
+const _ = imports.gettext.gettext;
+
 const Geocode = imports.gi.GeocodeGlib;
 const GLib = imports.gi.GLib;
 const GObject = imports.gi.GObject;
+
 const Location = imports.location;
+const Overpass = imports.overpass;
 const Translations = imports.translations;
 const Utils = imports.utils;
 
@@ -38,6 +42,14 @@ const DMS_COORDINATES_REGEX = new RegExp(
     "i"
 );
 
+// Matches a URL pointing to an object in OpenStreetMap
+const OSM_OBJECT_URL_REGEX =
+    new RegExp(/https?:\/\/(www\.)?openstreetmap\.org\/(node|way|relation)\/(\d+)\/?$/);
+
+// Matches a URL with a specifies coordinate in OpenStreetMap
+const OSM_COORD_URL_REGEX =
+    new 
RegExp(/https?:\/\/(www\.)?openstreetmap\.org\/?\?(\&?mlat=(\d+(?:\.\d+)?))?(\&mlon=(\d+(?:\.\d+)?))?(\&zoom=(\d+))?(\#map=(\d+)\/(\d+(?:\.\d+)?)\/(\d+(?:\.\d+)?))?/);
+
 var Place = GObject.registerClass(
 class Place extends Geocode.Place {
 
@@ -434,3 +446,79 @@ Place.parseCoordinates = function(text) {
         return null;
     }
 };
+
+let overpass = null;
+
+/* we can't import Application before the Place class has been defined
+ * since it's used via PlaceStore
+ */
+const Application = imports.application;
+
+function _parseOSMObjectURL(match, callback) {
+    let [,, type, id] = match;
+    let storedPlace = Application.placeStore.existsWithOsmTypeAndId(type, id);
+
+    if (storedPlace) {
+        callback(storedPlace, null);
+        return;
+    }
+
+    if (overpass === null)
+        overpass = new Overpass.Overpass();
+
+    Application.application.mark_busy();
+    overpass.fetchPlace(type, id, (place) => {
+        Application.application.unmark_busy();
+        if (place)
+            callback(place, null);
+        else
+            callback(null, _("Place not found in OpenStreetMap"));
+    });
+}
+
+function _parseOSMCoordURL(match, callback) {
+    let [,,,mlat,,mlon,,zoom,,mapZoom,mapLat, mapLon] = match;
+    let lat;
+    let lon;
+    let z;
+
+    if (mapZoom && mapLat && mapLon) {
+        lat = mapLat;
+        lon = mapLon;
+        z = mapZoom;
+    } else if (mlat && mlon) {
+        lat = mlat;
+        lon = mlon;
+        if (zoom)
+            z = zoom;
+    } else {
+        callback(null, _("OpenStreetMap URL is not valid"));
+        return;
+    }
+
+    if (!Place.validateCoordinates(lat, lon)) {
+        callback(null, _("Coordinates in URL are not valid"));
+        return;
+    }
+
+    let location = new Location.Location({ latitude: lat, longitude: lon });
+    let place = z ? new Place({ location: location, initialZoom: z }) :
+                    new Place({ location: location });
+
+    callback(place, null);
+}
+
+function parseHttpURL(text, callback) {
+    let match = text.match(OSM_OBJECT_URL_REGEX);
+
+    if (match) {
+        _parseOSMObjectURL(match, callback);
+    } else {
+        match = text.match(OSM_COORD_URL_REGEX);
+        if (match)
+            _parseOSMCoordURL(match, callback);
+        else
+            callback(null, _("URL is not supported"));
+    }
+}
+


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]