Parsing GeoJSON FeatureCollection
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
goog.provide('ol.io.geojson');
|
goog.provide('ol.io.geojson');
|
||||||
|
|
||||||
|
goog.require('ol.Feature');
|
||||||
goog.require('ol.geom.Geometry');
|
goog.require('ol.geom.Geometry');
|
||||||
goog.require('ol.geom.LineString');
|
goog.require('ol.geom.LineString');
|
||||||
goog.require('ol.geom.MultiLineString');
|
goog.require('ol.geom.MultiLineString');
|
||||||
@@ -12,7 +13,8 @@ goog.require('ol.geom.Polygon');
|
|||||||
/**
|
/**
|
||||||
* Parse a GeoJSON string.
|
* Parse a GeoJSON string.
|
||||||
* @param {string} str GeoJSON string.
|
* @param {string} str GeoJSON string.
|
||||||
* @return {ol.geom.Geometry|Array.<ol.geom.Geometry>} Parsed geometry or array
|
* @return {ol.Feature|Array.<ol.Feature>|
|
||||||
|
* ol.geom.Geometry|Array.<ol.geom.Geometry>} Parsed geometry or array
|
||||||
* of geometries.
|
* of geometries.
|
||||||
*/
|
*/
|
||||||
ol.io.geojson.read = function(str) {
|
ol.io.geojson.read = function(str) {
|
||||||
@@ -24,13 +26,22 @@ ol.io.geojson.read = function(str) {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {GeoJSONObject} json GeoJSON object.
|
* @param {GeoJSONObject} json GeoJSON object.
|
||||||
* @return {ol.geom.Geometry|Array.<ol.geom.Geometry>} Parsed geometry or array
|
* @return {ol.Feature|Array.<ol.Feature>|
|
||||||
|
* ol.geom.Geometry|Array.<ol.geom.Geometry>} Parsed geometry or array
|
||||||
* of geometries.
|
* of geometries.
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
ol.io.geojson.parse_ = function(json) {
|
ol.io.geojson.parse_ = function(json) {
|
||||||
var result;
|
var result;
|
||||||
switch (json.type) {
|
switch (json.type) {
|
||||||
|
case 'FeatureCollection':
|
||||||
|
result = ol.io.geojson.parseFeatureCollection_(
|
||||||
|
/** @type {GeoJSONFeatureCollection} */ (json));
|
||||||
|
break;
|
||||||
|
case 'Feature':
|
||||||
|
result = ol.io.geojson.parseFeature_(
|
||||||
|
/** @type {GeoJSONFeature} */ (json));
|
||||||
|
break;
|
||||||
case 'GeometryCollection':
|
case 'GeometryCollection':
|
||||||
result = ol.io.geojson.parseGeometryCollection_(
|
result = ol.io.geojson.parseGeometryCollection_(
|
||||||
/** @type {GeoJSONGeometryCollection} */ (json));
|
/** @type {GeoJSONGeometryCollection} */ (json));
|
||||||
@@ -66,6 +77,41 @@ ol.io.geojson.parse_ = function(json) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {GeoJSONFeature} json GeoJSON feature.
|
||||||
|
* @return {ol.Feature} Parsed feature.
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
ol.io.geojson.parseFeature_ = function(json) {
|
||||||
|
var geomJson = json.geometry,
|
||||||
|
geometry;
|
||||||
|
if (geomJson) {
|
||||||
|
geometry = /** @type {ol.geom.Geometry} */ (ol.io.geojson.parse_(
|
||||||
|
/** @type {GeoJSONGeometry} */ (geomJson)));
|
||||||
|
}
|
||||||
|
return new ol.Feature(geometry, json.properties);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {GeoJSONFeatureCollection} json GeoJSON feature collection.
|
||||||
|
* @return {Array.<ol.Feature>} Parsed array of features.
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
ol.io.geojson.parseFeatureCollection_ = function(json) {
|
||||||
|
var features = json.features,
|
||||||
|
len = features.length,
|
||||||
|
result = new Array(len),
|
||||||
|
i;
|
||||||
|
|
||||||
|
for (i = 0; i < len; ++i) {
|
||||||
|
result[i] = ol.io.geojson.parse_(
|
||||||
|
/** @type {GeoJSONFeature} */ (features[i]));
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {GeoJSONGeometryCollection} json GeoJSON geometry collection.
|
* @param {GeoJSONGeometryCollection} json GeoJSON geometry collection.
|
||||||
* @return {Array.<ol.geom.Geometry>} Parsed array of geometries.
|
* @return {Array.<ol.geom.Geometry>} Parsed array of geometries.
|
||||||
|
|||||||
@@ -1,5 +1,69 @@
|
|||||||
describe('ol.io.geojson', function() {
|
describe('ol.io.geojson', function() {
|
||||||
|
|
||||||
|
var data = {
|
||||||
|
'type': 'FeatureCollection',
|
||||||
|
'features': [
|
||||||
|
{
|
||||||
|
'type': 'Feature',
|
||||||
|
'properties': {
|
||||||
|
'LINK_ID': 573730499,
|
||||||
|
'RP_TYPE': 14,
|
||||||
|
'RP_FUNC': 0,
|
||||||
|
'DIRECTION': 2,
|
||||||
|
'LOGKOD': '',
|
||||||
|
'CHANGED': '',
|
||||||
|
'USERID': '',
|
||||||
|
'ST_NAME': '',
|
||||||
|
'L_REFADDR': '',
|
||||||
|
'L_NREFADDR': '',
|
||||||
|
'R_REFADDR': '',
|
||||||
|
'R_NREFADDR': '',
|
||||||
|
'SPEED_CAT': '7',
|
||||||
|
'ZIPCODE': '59330',
|
||||||
|
'SHAPE_LEN': 46.3826
|
||||||
|
},
|
||||||
|
'geometry': {
|
||||||
|
'type': 'LineString',
|
||||||
|
'coordinates': [
|
||||||
|
[1549497.66985, 6403707.96],
|
||||||
|
[1549491.1, 6403710.1],
|
||||||
|
[1549488.03995, 6403716.7504],
|
||||||
|
[1549488.5401, 6403724.5504],
|
||||||
|
[1549494.37985, 6403733.54],
|
||||||
|
[1549499.6799, 6403738.0504],
|
||||||
|
[1549506.22, 6403739.2504]
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}, {
|
||||||
|
'type': 'Feature',
|
||||||
|
'properties': {
|
||||||
|
'LINK_ID': 30760556,
|
||||||
|
'RP_TYPE': 12,
|
||||||
|
'RP_FUNC': 1,
|
||||||
|
'DIRECTION': 0,
|
||||||
|
'LOGKOD': '',
|
||||||
|
'CHANGED': '',
|
||||||
|
'USERID': '',
|
||||||
|
'ST_NAME': 'BRUNNSGATAN',
|
||||||
|
'L_REFADDR': '24',
|
||||||
|
'L_NREFADDR': '16',
|
||||||
|
'R_REFADDR': '',
|
||||||
|
'R_NREFADDR': '',
|
||||||
|
'SPEED_CAT': '7',
|
||||||
|
'ZIPCODE': '59330',
|
||||||
|
'SHAPE_LEN': 70.3106
|
||||||
|
},
|
||||||
|
'geometry': {
|
||||||
|
'type': 'LineString',
|
||||||
|
'coordinates': [
|
||||||
|
[1549754.2769, 6403854.8024],
|
||||||
|
[1549728.45985, 6403920.2]
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
};
|
||||||
|
|
||||||
describe('read()', function() {
|
describe('read()', function() {
|
||||||
|
|
||||||
it('parses point', function() {
|
it('parses point', function() {
|
||||||
@@ -60,6 +124,25 @@ describe('ol.io.geojson', function() {
|
|||||||
expect(array[1]).toBeA(ol.geom.LineString);
|
expect(array[1]).toBeA(ol.geom.LineString);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('parses feature collection', function() {
|
||||||
|
var str = JSON.stringify(data),
|
||||||
|
array = ol.io.geojson.read(str);
|
||||||
|
|
||||||
|
expect(array.length).toBe(2);
|
||||||
|
|
||||||
|
var first = array[0];
|
||||||
|
expect(first).toBeA(ol.Feature);
|
||||||
|
expect(first.get('LINK_ID')).toBe(573730499);
|
||||||
|
var firstGeom = first.getGeometry();
|
||||||
|
expect(firstGeom).toBeA(ol.geom.LineString);
|
||||||
|
|
||||||
|
var second = array[1];
|
||||||
|
expect(second).toBeA(ol.Feature);
|
||||||
|
expect(second.get('ST_NAME')).toBe('BRUNNSGATAN');
|
||||||
|
var secondGeom = second.getGeometry();
|
||||||
|
expect(secondGeom).toBeA(ol.geom.LineString);
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user