[gnome-maps/wip/mlundblad/get-extents-from-photon] photonParser: Parse bounding box when available




commit 9628ed095f60330fe81134bf4833f21935418bee
Author: Marcus Lundblad <ml update uu se>
Date:   Mon Oct 5 23:52:44 2020 +0200

    photonParser: Parse bounding box when available
    
    Parse feature's bounding boxes from the "extents"
    array when available in the result.
    This allows MapWalker to get a better zoom-to-fit
    for some features, compared to the fixed zoom levels
    based on types.

 src/photonParser.js | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
---
diff --git a/src/photonParser.js b/src/photonParser.js
index 3c3a78b6..4ae3e42c 100644
--- a/src/photonParser.js
+++ b/src/photonParser.js
@@ -85,6 +85,9 @@ function parsePlace(latitude, longitude, properties) {
     if (!countryCode && properties.country)
         place.country = properties.country;
 
+    if (properties.extent)
+        place.bounding_box = _parseBoundingBox(properties.extent);
+
     return new Place.Place({ place: place });
 }
 
@@ -206,3 +209,34 @@ function _parsePlaceType(properties) {
             return Geocode.PlaceType.MISCELLANEOUS;
     }
 }
+
+function _parseBoundingBox(extent) {
+    if (!Array.isArray(extent) || extent.length !== 4 ||
+        !_isValidLongitude(extent[0]) || !_isValidLatitude(extent[1]) ||
+        !_isValidLongitude(extent[2]) || !_isValidLatitude(extent[3])) {
+        Utils.debug('invalid extents in response: ' +
+                    JSON.stringify(extent, null, 2));
+        return null;
+    }
+
+    /* it seems GraphHopper geocode swaps order of bottom and top compared
+     * to stock Photon, so just in case "clamp" both pairs
+     */
+    return new Geocode.BoundingBox({ left:   Math.min(extent[0], extent[2]),
+                                     bottom: Math.min(extent[1], extent[3]),
+                                     right:  Math.max(extent[0], extent[2]),
+                                     top:    Math.max(extent[1], extent[3]) });
+}
+
+/* check if an extent value is a valid latitude (clamp to the maximum latitude
+ * supported by the map view
+ */
+function _isValidLatitude(number) {
+    return Number.isFinite(number) && number >= -85.0511287798 &&
+           number <= 85.0511287798;
+}
+
+// check if an extent value is a valid longitude
+function _isValidLongitude(number) {
+    return Number.isFinite(number) && number >= -180 && number <= 180;
+}


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