Skip to content

Commit 50e41ba

Browse files
committed
extend addData method NOT WORK
1 parent 94be955 commit 50e41ba

4 files changed

Lines changed: 126 additions & 45 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ The algorithm is documented in [Google Maps API Docs](https://developers.google.
1010
- [pip](https://pip.pypa.io/en/latest/installing.html)
1111
- [flask](http://flask.pocoo.org/)
1212
- [flask-cors](http://flask-cors.readthedocs.org/en/latest/)
13+
- [watchdog](https://pypi.python.org/pypi/watchdog)
1314

1415

1516
### Installation

‎examples/index.html‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ <h4>Simple Example</h4>
3333
<div class="maps" id="map1"></div>
3434
<div class="maps" id="map2"></div>
3535
</div>
36-
<script src="../node_modules/leaflet/dist/leaflet.js"></script>
37-
<script src="../node_modules/polyline-encoded/Polyline.encoded.js"></script>
3836
<script src="../node_modules/jquery/dist/jquery.min.js"></script>
37+
<script src="../node_modules/leaflet/dist/leaflet-src.js"></script>
38+
<script src="../node_modules/polyline-encoded/Polyline.encoded.js"></script>
3939
<script src="../src/leaflet.geojson.encoded.js"></script>
4040
<script>
4141

@@ -53,8 +53,8 @@ <h4>Simple Example</h4>
5353
});
5454

5555
$.getJSON('http://localhost:8000/encode/geojson_test.json', function(data) {
56-
57-
var geoEnc = L.GeoJSON.Encoded(data);
56+
57+
var geoEnc = new L.GeoJSON.Encoded(data);
5858

5959
map2.addLayer(geoEnc).fitBounds( geoEnc.getBounds() );
6060
});

‎requirements.txt‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
flask
2-
flask-cors
2+
flask-cors
3+
watchdog

‎src/leaflet.geojson.encoded.js‎

Lines changed: 119 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,103 @@
11
(function() {
22

3-
function _build_linestrings(geom) {
3+
L.GeoJSON.Encoded = L.GeoJSON.extend({
44

5-
var paths = [];
6-
for (var j = 0; j < geom.length; j++) {
7-
paths.push(L.PolylineUtil.decode(geom[j]));
8-
}
9-
return paths;
10-
}
5+
_decodeFeature: function(feature) {
116

12-
function _build_polygons(geom) {
13-
var polygons = [];
14-
for (var i = 0; i < geom.length; i++) {
15-
polygons.push(_build_linestrings(geom[i]));
16-
}
17-
return polygons;
18-
}
7+
var resp;
198

20-
function decodeToLayer(featureCollection, targetLayer, style, onEachFeature) {
21-
var geom,bb,
22-
features = featureCollection["features"],
23-
popupOpts = {closeButton: false};
9+
function _build_linestrings(geom) {
10+
var paths = [];
11+
for (var j = 0; j < geom.length; j++)
12+
paths.push( L.PolylineUtil.decode(geom[j]) );
13+
return paths;
14+
}
2415

25-
for (var i = 0; i < features.length; i++) {
26-
var feature = features[i];
16+
function _build_polygons(geom) {
17+
var polygons = [];
18+
for (var i = 0; i < geom.length; i++)
19+
polygons.push( _build_linestrings(geom[i]) );
20+
return polygons;
21+
}
2722

28-
geom = feature["geometry"]["coordinates"];
23+
var geom = feature["geometry"]["coordinates"];
2924

3025
switch(feature["geometry"]["type"]) {
3126
case "Point":
32-
L.marker([geom[1], geom[0]]).addTo(targetLayer);
27+
28+
resp = L.marker([geom[1], geom[0]]);
29+
3330
break;
3431
case "LineString":
3532
var coords = L.Util.isArray(geom[0]) ? L.GeoJSON.coordsToLatLngs(geom, 0) : L.PolylineUtil.decode(geom);
3633

37-
var poly = L.polyline(coords, style).addTo(targetLayer);
38-
onEachFeature(feature, poly);
34+
resp = L.polyline(coords);
3935

4036
break;
4137
case "MultiLineString":
42-
var ls = L.Util.isArray(geom[0][0]) ? L.GeoJSON.coordsToLatLngs(geom, 1) : _build_linestrings(geom);
38+
var coords = L.Util.isArray(geom[0][0]) ? L.GeoJSON.coordsToLatLngs(geom, 1) : _build_linestrings(geom);
4339

44-
var poly = L.multiPolyline(ls, style).addTo(targetLayer);
45-
onEachFeature(feature, poly);
40+
resp = L.multiPolyline(coords);
4641

4742
break;
4843
case "Polygon":
4944
var rings = L.Util.isArray(geom[0][0]) ? L.GeoJSON.coordsToLatLngs(geom, 1) : _build_linestrings(geom);
5045

51-
var poly = L.polygon(rings, style).addTo(targetLayer);
52-
onEachFeature(feature, poly);
46+
resp = L.polygon(rings);
5347

5448
break;
5549
case "MultiPolygon":
5650
var polygons = L.Util.isArray(geom[0][0]) ? L.GeoJSON.coordsToLatLngs(geom, 2) : _build_polygons(geom);
5751

58-
var poly = L.multiPolygon(polygons, style).addTo(targetLayer);
59-
onEachFeature(feature, poly);
52+
resp = L.multiPolygon(polygons);
6053

6154
break;
62-
default:
63-
console.error(feature.geometry.type + ' not implemented.');
6455
}
65-
}
66-
return targetLayer;
67-
}
6856

69-
L.GeoJSON.Encoded = L.GeoJSON.extend({
57+
return resp.toGeoJSON();
58+
},
59+
60+
addData: function (geojsonEnc) {
7061

71-
/* addData: function (geojson) {
62+
var features = L.Util.isArray(geojsonEnc) ? geojsonEnc : geojsonEnc.features,
63+
i, len, feature;
64+
65+
if (features) {
66+
for (i = 0, len = features.length; i < len; i++) {
67+
// only add this if geometry or geometries are set and not null
68+
69+
feature = this._decodeFeature(features[i]);
70+
//feature = features[i];
71+
72+
console.log(features[i], feature);
73+
74+
if (feature.geometries || feature.geometry || feature.features || feature.coordinates) {
75+
this.addData(feature);
76+
}
77+
}
78+
return this;
79+
}
80+
81+
var options = this.options;
82+
83+
if (options.filter && !options.filter(geojsonEnc)) { return this; }
84+
85+
var layer = L.GeoJSON.geometryToLayer(geojsonEnc, options);
86+
layer.feature = L.GeoJSON.asFeature(geojsonEnc);
87+
88+
layer.defaultOptions = layer.options;
89+
this.resetStyle(layer);
90+
91+
if (options.onEachFeature) {
92+
options.onEachFeature(geojsonEnc, layer);
93+
}
94+
95+
return this.addLayer(layer);
96+
//return L.GeoJSON.prototype.addData.call(this, geojsonEnc);
97+
}
98+
// */
99+
/* DEFAULT GeoJSON METHOD
100+
addData: function (geojson) {
72101
var features = L.Util.isArray(geojson) ? geojson : geojson.features,
73102
i, len, feature;
74103
@@ -100,9 +129,59 @@ L.GeoJSON.Encoded = L.GeoJSON.extend({
100129
return this.addLayer(layer);
101130
}*/
102131
});
132+
/*
133+
134+
L.extend(L.GeoJSON.Encoded, {
135+
geometryToLayer: function (geojson, options) {
136+
137+
var geometry = geojson.type === 'Feature' ? geojson.geometry : geojson,
138+
coords = geometry.coordinates,
139+
layers = [],
140+
pointToLayer = options && options.pointToLayer,
141+
coordsToLatLng = options && options.coordsToLatLng || this.coordsToLatLng,
142+
latlng, latlngs, i, len;
143+
144+
switch (geometry.type) {
145+
case 'Point':
146+
latlng = coordsToLatLng(coords);
147+
return pointToLayer ? pointToLayer(geojson, latlng) : new L.Marker(latlng);
148+
149+
case 'MultiPoint':
150+
for (i = 0, len = coords.length; i < len; i++) {
151+
latlng = coordsToLatLng(coords[i]);
152+
layers.push(pointToLayer ? pointToLayer(geojson, latlng) : new L.Marker(latlng));
153+
}
154+
return new L.FeatureGroup(layers);
155+
156+
case 'LineString':
157+
case 'MultiLineString':
158+
latlngs = this.coordsToLatLngs(coords, geometry.type === 'LineString' ? 0 : 1, coordsToLatLng);
159+
return new L.Polyline(latlngs, options);
160+
161+
case 'Polygon':
162+
case 'MultiPolygon':
163+
latlngs = this.coordsToLatLngs(coords, geometry.type === 'Polygon' ? 1 : 2, coordsToLatLng);
164+
return new L.Polygon(latlngs, options);
165+
166+
case 'GeometryCollection':
167+
for (i = 0, len = geometry.geometries.length; i < len; i++) {
168+
169+
layers.push(this.geometryToLayer({
170+
geometry: geometry.geometries[i],
171+
type: 'Feature',
172+
properties: geojson.properties
173+
}, options));
174+
}
175+
return new L.FeatureGroup(layers);
176+
177+
default:
178+
throw new Error('Invalid GeoJSON object.');
179+
}
180+
}
181+
});*/
103182

104-
L.geoJson.encoded = function (options) {
105-
return new L.GeoJSON.Encoded(options);
183+
L.geoJson.encoded = function (geojson, options) {
184+
return new L.GeoJSON.Encoded(geojson, options);
106185
};
107186

108187
}).call(this);

0 commit comments

Comments
 (0)