aboutsummaryrefslogtreecommitdiffstats
path: root/extlib/leaflet/src/layer/GeoJSON.js
blob: 6cbd419325de0b8c4672f00fe48d3c662ddceae0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
L.GeoJSON = L.LayerGroup.extend({
	includes: L.Mixin.Events,
	
	initialize: function(geojson, options) {
		L.Util.setOptions(this, options);
		this._geojson = geojson;
		this._layers = {};
		
		if (geojson) {
			this.addGeoJSON(geojson);
		}
	},
	
	addGeoJSON: function(geojson) {
		if (geojson.features) {
			for (var i = 0, len = geojson.features.length; i < len; i++) {
				this.addGeoJSON(geojson.features[i]);
			}
			return;
		}
				
		var isFeature = (geojson.type == 'Feature'),
			geometry = (isFeature ? geojson.geometry : geojson),
			layer = L.GeoJSON.geometryToLayer(geometry, this.options.pointToLayer);
		
		this.fire('featureparse', {
			layer: layer, 
			properties: geojson.properties,
			geometryType: geometry.type,
			bbox: geojson.bbox,
			id: geojson.id
		});
		
		this.addLayer(layer);
	}
});

L.Util.extend(L.GeoJSON, {
	geometryToLayer: function(geometry, pointToLayer) {
		var coords = geometry.coordinates, 
			latlng, latlngs, 
			i, len, 
			layer, 
			layers = [];

		switch (geometry.type) {
			case 'Point':
				latlng = this.coordsToLatLng(coords);
				return pointToLayer ? pointToLayer(latlng) : new L.Marker(latlng);
				
			case 'MultiPoint':
				for (i = 0, len = coords.length; i < len; i++) {
					latlng = this.coordsToLatLng(coords[i]);
					layer = pointToLayer ? pointToLayer(latlng) : new L.Marker(latlng);
					layers.push(layer);
				}
				return new L.FeatureGroup(layers);
				
			case 'LineString':
				latlngs = this.coordsToLatLngs(coords);
				return new L.Polyline(latlngs);
				
			case 'Polygon':
				latlngs = this.coordsToLatLngs(coords, 1);
				return new L.Polygon(latlngs);
				
			case 'MultiLineString':
				latlngs = this.coordsToLatLngs(coords, 1);
				return new L.MultiPolyline(latlngs);
				
			case "MultiPolygon":
				latlngs = this.coordsToLatLngs(coords, 2);
				return new L.MultiPolygon(latlngs);
				
			case "GeometryCollection":
				for (i = 0, len = geometry.geometries.length; i < len; i++) {
					layer = this.geometryToLayer(geometry.geometries[i]);
					layers.push(layer);
				}
				return new L.FeatureGroup(layers);
				
			default:
				throw new Error('Invalid GeoJSON object.');
		}
	},

	coordsToLatLng: function(/*Array*/ coords, /*Boolean*/ reverse)/*: LatLng*/ {
		var lat = parseFloat(coords[reverse ? 0 : 1]),
			lng = parseFloat(coords[reverse ? 1 : 0]);
		return new L.LatLng(lat, lng);
	},

	coordsToLatLngs: function(/*Array*/ coords, /*Number*/ levelsDeep, /*Boolean*/ reverse)/*: Array*/ {
		var latlng, latlngs = [],
			i, len = coords.length;
		
		for (i = 0; i < len; i++) {
			latlng = levelsDeep ? 
					this.coordsToLatLngs(coords[i], levelsDeep - 1, reverse) : 
					this.coordsToLatLng(coords[i], reverse);
			latlngs.push(latlng);
		}
		return latlngs;
	}
});