Add geojson module in io package
This commit is contained in:
@@ -39,7 +39,9 @@
|
|||||||
"disambiguate-properties": true,
|
"disambiguate-properties": true,
|
||||||
|
|
||||||
"externs": [
|
"externs": [
|
||||||
|
"//json.js",
|
||||||
"externs/bingmaps.js",
|
"externs/bingmaps.js",
|
||||||
|
"externs/geojson.js",
|
||||||
"externs/proj4js.js",
|
"externs/proj4js.js",
|
||||||
"externs/tilejson.js"
|
"externs/tilejson.js"
|
||||||
],
|
],
|
||||||
|
|||||||
+32
-32
@@ -6,17 +6,29 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @constructor
|
* @constructor
|
||||||
*/
|
*/
|
||||||
var GeoJSONCRS = function() {};
|
var GeoJSONObject = function() {};
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @type {string}
|
* @type {string}
|
||||||
*/
|
*/
|
||||||
GeoJSONCRS.prototype.type;
|
GeoJSONObject.prototype.type;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type {!GeoJSONCRS|undefined}
|
||||||
|
*/
|
||||||
|
GeoJSONObject.prototype.crs;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @constructor
|
||||||
|
* @extends {GeoJSONObject}
|
||||||
|
*/
|
||||||
|
var GeoJSONCRS = function() {};
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -28,35 +40,38 @@ GeoJSONCRS.prototype.properties;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @constructor
|
* @constructor
|
||||||
|
* @extends {GeoJSONObject}
|
||||||
*/
|
*/
|
||||||
var GeoJSONGeometry = function() {};
|
var GeoJSONGeometry = function() {};
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @type {string}
|
* @type {!Array.<number>|!Array.<!Array.<number>>|
|
||||||
*/
|
* !Array.<!Array.<!Array.<number>>>}
|
||||||
GeoJSONGeometry.prototype.type;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @type {!Array.<number>|!Array.<!Array.<number>>}
|
|
||||||
*/
|
*/
|
||||||
GeoJSONGeometry.prototype.coordinates;
|
GeoJSONGeometry.prototype.coordinates;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @constructor
|
||||||
|
* @extends {GeoJSONObject}
|
||||||
|
*/
|
||||||
|
var GeoJSONGeometryCollection = function() {};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type {!Array.<GeoJSONGeometry>}
|
||||||
|
*/
|
||||||
|
GeoJSONGeometryCollection.prototype.geometries;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @constructor
|
* @constructor
|
||||||
|
* @extends {GeoJSONObject}
|
||||||
*/
|
*/
|
||||||
var GeoJSONFeature = function() {};
|
var GeoJSONFeature = function() {};
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @type {string}
|
|
||||||
*/
|
|
||||||
GeoJSONFeature.prototype.type;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @type {GeoJSONGeometry}
|
* @type {GeoJSONGeometry}
|
||||||
*/
|
*/
|
||||||
@@ -72,16 +87,11 @@ GeoJSONFeature.prototype.properties;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @constructor
|
* @constructor
|
||||||
|
* @extends {GeoJSONObject}
|
||||||
*/
|
*/
|
||||||
var GeoJSONFeatureCollection = function() {};
|
var GeoJSONFeatureCollection = function() {};
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @type {string}
|
|
||||||
*/
|
|
||||||
GeoJSONFeatureCollection.prototype.type;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @type {!Array.<GeoJSONFeature>}
|
* @type {!Array.<GeoJSONFeature>}
|
||||||
*/
|
*/
|
||||||
@@ -94,13 +104,3 @@ GeoJSONFeatureCollection.prototype.features;
|
|||||||
GeoJSONFeatureCollection.prototype.bbox;
|
GeoJSONFeatureCollection.prototype.bbox;
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @type {!GeoJSONCRS|undefined}
|
|
||||||
*/
|
|
||||||
GeoJSONFeatureCollection.prototype.crs;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @type {!Object.<string, *>}
|
|
||||||
*/
|
|
||||||
GeoJSONFeatureCollection.prototype.properties;
|
|
||||||
|
|||||||
@@ -0,0 +1,145 @@
|
|||||||
|
goog.provide('ol.io.geojson');
|
||||||
|
|
||||||
|
goog.require('ol.geom.Geometry');
|
||||||
|
goog.require('ol.geom.LineString');
|
||||||
|
goog.require('ol.geom.MultiLineString');
|
||||||
|
goog.require('ol.geom.MultiPoint');
|
||||||
|
goog.require('ol.geom.MultiPolygon');
|
||||||
|
goog.require('ol.geom.Point');
|
||||||
|
goog.require('ol.geom.Polygon');
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parse a GeoJSON string.
|
||||||
|
* @param {string} str GeoJSON string.
|
||||||
|
* @return {ol.geom.Geometry|Array.<ol.geom.Geometry>} Parsed geometry or array
|
||||||
|
* of geometries.
|
||||||
|
*/
|
||||||
|
ol.io.geojson.read = function(str) {
|
||||||
|
// TODO: add options and accept projection
|
||||||
|
var json = /** @type {GeoJSONObject} */ (JSON.parse(str));
|
||||||
|
return ol.io.geojson.parse_(json);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {GeoJSONObject} json GeoJSON object.
|
||||||
|
* @return {ol.geom.Geometry|Array.<ol.geom.Geometry>} Parsed geometry or array
|
||||||
|
* of geometries.
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
ol.io.geojson.parse_ = function(json) {
|
||||||
|
var result;
|
||||||
|
switch (json.type) {
|
||||||
|
case 'GeometryCollection':
|
||||||
|
result = ol.io.geojson.parseGeometryCollection_(
|
||||||
|
/** @type {GeoJSONGeometryCollection} */ (json));
|
||||||
|
break;
|
||||||
|
case 'Point':
|
||||||
|
result = ol.io.geojson.parsePoint_(
|
||||||
|
/** @type {GeoJSONGeometry} */ (json));
|
||||||
|
break;
|
||||||
|
case 'LineString':
|
||||||
|
result = ol.io.geojson.parseLineString_(
|
||||||
|
/** @type {GeoJSONGeometry} */ (json));
|
||||||
|
break;
|
||||||
|
case 'Polygon':
|
||||||
|
result = ol.io.geojson.parsePolygon_(
|
||||||
|
/** @type {GeoJSONGeometry} */ (json));
|
||||||
|
break;
|
||||||
|
case 'MultiPoint':
|
||||||
|
result = ol.io.geojson.parseMultiPoint_(
|
||||||
|
/** @type {GeoJSONGeometry} */ (json));
|
||||||
|
break;
|
||||||
|
case 'MultiLineString':
|
||||||
|
result = ol.io.geojson.parseMultiLineString_(
|
||||||
|
/** @type {GeoJSONGeometry} */ (json));
|
||||||
|
break;
|
||||||
|
case 'MultiPolygon':
|
||||||
|
result = ol.io.geojson.parseMultiPolygon_(
|
||||||
|
/** @type {GeoJSONGeometry} */ (json));
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new Error('GeoJSON parsing not implemented for type: ' + json.type);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {GeoJSONGeometryCollection} json GeoJSON geometry collection.
|
||||||
|
* @return {Array.<ol.geom.Geometry>} Parsed array of geometries.
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
ol.io.geojson.parseGeometryCollection_ = function(json) {
|
||||||
|
var geometries = json.geometries,
|
||||||
|
len = geometries.length,
|
||||||
|
result = new Array(len),
|
||||||
|
i;
|
||||||
|
|
||||||
|
for (i = 0; i < len; ++i) {
|
||||||
|
result[i] = ol.io.geojson.parse_(
|
||||||
|
/** @type {GeoJSONGeometry} */ (geometries[i]));
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {GeoJSONGeometry} json GeoJSON linestring.
|
||||||
|
* @return {ol.geom.LineString} Parsed linestring.
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
ol.io.geojson.parseLineString_ = function(json) {
|
||||||
|
return new ol.geom.LineString(json.coordinates);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {GeoJSONGeometry} json GeoJSON multi-linestring.
|
||||||
|
* @return {ol.geom.MultiLineString} Parsed multi-linestring.
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
ol.io.geojson.parseMultiLineString_ = function(json) {
|
||||||
|
return new ol.geom.MultiLineString(json.coordinates);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {GeoJSONGeometry} json GeoJSON multi-point.
|
||||||
|
* @return {ol.geom.MultiPoint} Parsed multi-point.
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
ol.io.geojson.parseMultiPoint_ = function(json) {
|
||||||
|
return new ol.geom.MultiPoint(json.coordinates);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {GeoJSONGeometry} json GeoJSON multi-polygon.
|
||||||
|
* @return {ol.geom.MultiPolygon} Parsed multi-polygon.
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
ol.io.geojson.parseMultiPolygon_ = function(json) {
|
||||||
|
return new ol.geom.MultiPolygon(json.coordinates);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {GeoJSONGeometry} json GeoJSON point.
|
||||||
|
* @return {ol.geom.Point} Parsed multi-point.
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
ol.io.geojson.parsePoint_ = function(json) {
|
||||||
|
return new ol.geom.Point(json.coordinates);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {GeoJSONGeometry} json GeoJSON polygon.
|
||||||
|
* @return {ol.geom.Polygon} Parsed polygon.
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
ol.io.geojson.parsePolygon_ = function(json) {
|
||||||
|
return new ol.geom.Polygon(json.coordinates);
|
||||||
|
};
|
||||||
@@ -80,6 +80,7 @@
|
|||||||
<script type="text/javascript" src="spec/ol/rectangle.test.js"></script>
|
<script type="text/javascript" src="spec/ol/rectangle.test.js"></script>
|
||||||
<script type="text/javascript" src="spec/ol/resolutionconstraint.test.js"></script>
|
<script type="text/javascript" src="spec/ol/resolutionconstraint.test.js"></script>
|
||||||
<script type="text/javascript" src="spec/ol/view2d.test.js"></script>
|
<script type="text/javascript" src="spec/ol/view2d.test.js"></script>
|
||||||
|
<script type="text/javascript" src="spec/ol/io/geojson.test.js"></script>
|
||||||
<script type="text/javascript" src="spec/ol/geom/linearring.test.js"></script>
|
<script type="text/javascript" src="spec/ol/geom/linearring.test.js"></script>
|
||||||
<script type="text/javascript" src="spec/ol/geom/linestring.test.js"></script>
|
<script type="text/javascript" src="spec/ol/geom/linestring.test.js"></script>
|
||||||
<script type="text/javascript" src="spec/ol/geom/point.test.js"></script>
|
<script type="text/javascript" src="spec/ol/geom/point.test.js"></script>
|
||||||
|
|||||||
@@ -0,0 +1,65 @@
|
|||||||
|
describe('ol.io.geojson', function() {
|
||||||
|
|
||||||
|
describe('read()', function() {
|
||||||
|
|
||||||
|
it('parses point', function() {
|
||||||
|
var str = JSON.stringify({
|
||||||
|
type: 'Point',
|
||||||
|
coordinates: [10, 20]
|
||||||
|
});
|
||||||
|
|
||||||
|
var obj = ol.io.geojson.read(str);
|
||||||
|
expect(obj).toBeA(ol.geom.Point);
|
||||||
|
expect(obj.coordinates[0]).toBe(10);
|
||||||
|
expect(obj.coordinates[1]).toBe(20);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('parses linestring', function() {
|
||||||
|
var str = JSON.stringify({
|
||||||
|
type: 'LineString',
|
||||||
|
coordinates: [[10, 20], [30, 40]]
|
||||||
|
});
|
||||||
|
|
||||||
|
var obj = ol.io.geojson.read(str);
|
||||||
|
expect(obj).toBeA(ol.geom.LineString);
|
||||||
|
expect(obj.coordinates[0]).toBe(10);
|
||||||
|
expect(obj.coordinates[1]).toBe(20);
|
||||||
|
expect(obj.coordinates[2]).toBe(30);
|
||||||
|
expect(obj.coordinates[3]).toBe(40);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('parses polygon', function() {
|
||||||
|
var outer = [[0, 0], [10, 0], [10, 10], [0, 10], [0, 0]],
|
||||||
|
inner1 = [[1, 1], [2, 1], [2, 2], [1, 2], [1, 1]],
|
||||||
|
inner2 = [[8, 8], [9, 8], [9, 9], [8, 9], [8, 8]],
|
||||||
|
str = JSON.stringify({
|
||||||
|
type: 'Polygon',
|
||||||
|
coordinates: [outer, inner1, inner2]
|
||||||
|
});
|
||||||
|
|
||||||
|
var obj = ol.io.geojson.read(str);
|
||||||
|
expect(obj).toBeA(ol.geom.Polygon);
|
||||||
|
expect(obj.rings.length).toBe(3);
|
||||||
|
expect(obj.rings[0]).toBeA(ol.geom.LinearRing);
|
||||||
|
expect(obj.rings[1]).toBeA(ol.geom.LinearRing);
|
||||||
|
expect(obj.rings[2]).toBeA(ol.geom.LinearRing);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('parses geometry collection', function() {
|
||||||
|
var str = JSON.stringify({
|
||||||
|
type: 'GeometryCollection',
|
||||||
|
geometries: [
|
||||||
|
{type: 'Point', coordinates: [10, 20]},
|
||||||
|
{type: 'LineString', coordinates: [[30, 40], [50, 60]]}
|
||||||
|
]
|
||||||
|
});
|
||||||
|
|
||||||
|
var array = ol.io.geojson.read(str);
|
||||||
|
expect(array.length).toBe(2);
|
||||||
|
expect(array[0]).toBeA(ol.geom.Point);
|
||||||
|
expect(array[1]).toBeA(ol.geom.LineString);
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user