[gnome-maps] Add support for mapbox simplestyle for GeoJSON



commit e25c9940773f8ab10ac66012e8d59ef24e45d8d7
Author: Alaf Azam <alafazam gmail com>
Date:   Fri Dec 11 18:14:35 2015 +0530

    Add support for mapbox simplestyle for GeoJSON
    
    https://bugzilla.gnome.org/show_bug.cgi?id=757144

 src/geoJSONSource.js                 |   24 +++++----
 src/geoJSONStyle.js                  |   91 ++++++++++++++++++++++++++++++++++
 src/org.gnome.Maps.src.gresource.xml |    1 +
 3 files changed, 105 insertions(+), 11 deletions(-)
---
diff --git a/src/geoJSONSource.js b/src/geoJSONSource.js
index 8ae6297..820178b 100644
--- a/src/geoJSONSource.js
+++ b/src/geoJSONSource.js
@@ -29,6 +29,8 @@ const Location = imports.location;
 const Place = imports.place;
 const PlaceMarker = imports.placeMarker;
 const Utils = imports.utils;
+const GeoJSONStyle = imports.geoJSONStyle;
+
 
 const TILE_SIZE = 256;
 
@@ -36,11 +38,6 @@ const TileFeature = { POINT: 1,
                       LINESTRING: 2,
                       POLYGON: 3 };
 
-const PolygonColor = { RED: 0.37,
-                       GREEN: 0.62,
-                       BLUE: 0.87,
-                       ALPHA: 0.25 };
-
 const GeoJSONSource = new Lang.Class({
     Name: 'GeoJSONSource',
     Extends: Champlain.TileSource,
@@ -237,17 +234,22 @@ const GeoJSONSource = new Lang.Class({
             cr.setOperator(Cairo.Operator.CLEAR);
             cr.paint();
             cr.setOperator(Cairo.Operator.OVER);
-            cr.setLineWidth(1);
             cr.setFillRule(Cairo.FillRule.EVEN_ODD);
 
             tileJSON.features.forEach(function(feature) {
                 if (feature.type === TileFeature.POINT)
                     return;
 
+                let geoJSONStyleObj = GeoJSONStyle.GeoJSONStyle.parseSimpleStyle(feature.tags);
+
                 feature.geometry.forEach(function(geometry) {
                     let first = true;
                     cr.moveTo(0, 0);
-                    cr.setSourceRGB(0, 0, 0);
+                    cr.setLineWidth(geoJSONStyleObj.lineWidth);
+                    cr.setSourceRGBA(geoJSONStyleObj.color.red,
+                                     geoJSONStyleObj.color.green,
+                                     geoJSONStyleObj.color.blue,
+                                     geoJSONStyleObj.alpha);
 
                     geometry.forEach(function(coord) {
                         if (first) {
@@ -261,10 +263,10 @@ const GeoJSONSource = new Lang.Class({
                 if (feature.type === TileFeature.POLYGON) {
                     cr.closePath();
                     cr.strokePreserve();
-                    cr.setSourceRGBA(PolygonColor.RED,
-                                     PolygonColor.GREEN,
-                                     PolygonColor.BLUE,
-                                     PolygonColor.ALPHA);
+                    cr.setSourceRGBA(geoJSONStyleObj.fillColor.red,
+                                     geoJSONStyleObj.fillColor.green,
+                                     geoJSONStyleObj.fillColor.blue,
+                                     geoJSONStyleObj.fillAlpha);
                     cr.fill();
                 } else {
                     cr.stroke();
diff --git a/src/geoJSONStyle.js b/src/geoJSONStyle.js
new file mode 100644
index 0000000..30bd2f5
--- /dev/null
+++ b/src/geoJSONStyle.js
@@ -0,0 +1,91 @@
+/* -*- Mode: JS2; indent-tabs-mode: nil; js2-basic-offset: 4 -*- */
+/* vim: set et ts=4 sw=4: */
+/*
+ * GNOME Maps is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.
+ *
+ * GNOME Maps is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with GNOME Maps; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Author: Alaf Azam <alafazam gmail com>
+ */
+
+const Lang = imports.lang;
+
+const GeoJSONStyle = new Lang.Class({
+    Name: 'GeoJSONStyle',
+
+    _init: function(params) {
+
+        if (params.lineWidth || params.lineWidth === 0)
+            this.lineWidth = params.lineWidth;
+        else
+            this.lineWidth = 1;
+
+        if (params.alpha || params.alpha === 0)
+            this.alpha = params.alpha;
+        else
+            this.alpha = 1;
+
+        if (params.fillAlpha || params.fillAlpha === 0)
+            this.fillAlpha = params.fillAlpha;
+        else
+            this.fillAlpha = 0.25;
+
+        this.color = this._hexToColor(params.color) || { red: 0,
+                                                         green: 0,
+                                                         blue: 0 };        
+        this.fillColor =  this._hexToColor(params.fillColor) || { red: 0.37,
+                                                                  green: 0.62,
+                                                                  blue: 0.87 };
+    },
+
+    _hexToColor: function(colorString) {
+        let color = null;
+        
+        if (!colorString)
+            return null;
+
+        if (colorString.startsWith('#')) {
+            colorString = colorString.slice(1);
+        }
+
+        if (colorString.length === 3) {
+            colorString = colorString.match(/([0-9a-f]{3})$/i);
+            if (colorString) {
+                color = {
+                    red: (parseInt(colorString[0].chatAt(0), 16) * 0x11)/255,
+                    green: (parseInt(colorString[0].chatAt(1), 16) * 0x11)/255,
+                    blue: (parseInt(colorString[0].chatAt(2), 16) * 0x11)/255
+                };
+            }
+        } else {
+            colorString = colorString.match(/([0-9a-f]{6})$/i);
+            if (colorString) {
+                color = {
+                    red: parseInt(colorString[0].substr(0, 2), 16)/255,
+                    green: parseInt(colorString[0].substr(2, 2), 16)/255,
+                    blue: parseInt(colorString[0].substr(4, 2), 16)/255
+                };
+            }
+        }
+
+        return color;
+    }
+});
+
+GeoJSONStyle.parseSimpleStyle = function(tags) {
+    return  new GeoJSONStyle({ alpha: tags['stroke-opacity'],
+                               fillAlpha: tags['fill-opacity'],
+                               color: tags['stroke'],
+                               fillColor: tags['fill'],
+                               lineWidth: tags['stroke-width'] });
+}
diff --git a/src/org.gnome.Maps.src.gresource.xml b/src/org.gnome.Maps.src.gresource.xml
index 8d171f1..50791dd 100644
--- a/src/org.gnome.Maps.src.gresource.xml
+++ b/src/org.gnome.Maps.src.gresource.xml
@@ -17,6 +17,7 @@
     <file>geoclue.js</file>
     <file>geocodeService.js</file>
     <file>geoJSONSource.js</file>
+    <file>geoJSONStyle.js</file>
     <file>http.js</file>
     <file>layersPopover.js</file>
     <file>location.js</file>


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