Merge pull request #1353 from twpayne/vector-api-format

[vector-api] ol.format.Format
This commit is contained in:
Tom Payne
2013-12-11 05:37:33 -08:00
24 changed files with 1865 additions and 375 deletions

View File

@@ -5,15 +5,14 @@ goog.require('ol.View2D');
goog.require('ol.control');
goog.require('ol.control.ScaleLine');
goog.require('ol.control.ScaleLineUnits');
goog.require('ol.format.GeoJSON');
goog.require('ol.geom.LineString');
goog.require('ol.geom.Point');
goog.require('ol.layer.Tile');
goog.require('ol.layer.Vector');
goog.require('ol.proj');
goog.require('ol.shape');
goog.require('ol.source.GeoJSON');
goog.require('ol.source.TileWMS');
goog.require('ol.source.Vector');
goog.require('ol.style.Stroke');
goog.require('ol.style.Style');
@@ -23,6 +22,20 @@ var projection = ol.proj.configureProj4jsProjection({
extent: [485869.5728, 76443.1884, 837076.5648, 299941.7864]
});
var vectorSource = new ol.source.GeoJSON({
defaultProjection: projection,
url: 'data/mtbland.geojson'
});
var styleArray = [new ol.style.Style({
stroke: new ol.style.Stroke({
color: 'rgba(128,0,128,0.8)',
lineCap: 'round',
lineJoin: 'round',
width: 3
})
})];
var extent = [420000, 30000, 900000, 350000];
var layers = [
new ol.layer.Tile({
@@ -41,6 +54,12 @@ var layers = [
},
extent: extent
})
}),
new ol.layer.Vector({
source: vectorSource,
styleFunction: function(feature, resolution) {
return styleArray;
}
})
];
@@ -61,28 +80,6 @@ var map = new ol.Map({
})
});
var styleArray = [new ol.style.Style({
stroke: new ol.style.Stroke({
color: 'rgba(128,0,128,0.8)',
lineCap: 'round',
lineJoin: 'round',
width: 3
})
})];
var vectorSource = new ol.source.Vector();
$.getJSON('data/mtbland.geojson', function(data) {
var format = new ol.format.GeoJSON();
format.readObject(data, vectorSource.addFeature, vectorSource);
var vectorLayer = new ol.layer.Vector({
source: vectorSource,
styleFunction: function(feature) {
return styleArray;
}
});
map.getLayers().push(vectorLayer);
});
var point = null;
var line = null;
var displaySnap = function(coordinate) {

View File

@@ -2,14 +2,13 @@ goog.require('ol.Feature');
goog.require('ol.Map');
goog.require('ol.RendererHint');
goog.require('ol.View2D');
goog.require('ol.format.GeoJSON');
goog.require('ol.geom.LineString');
goog.require('ol.geom.Point');
goog.require('ol.layer.Tile');
goog.require('ol.layer.Vector');
goog.require('ol.shape');
goog.require('ol.source.GeoJSON');
goog.require('ol.source.OSM');
goog.require('ol.source.Vector');
goog.require('ol.style.Fill');
goog.require('ol.style.Stroke');
goog.require('ol.style.Style');
@@ -62,63 +61,71 @@ var styleFunction = function(feature, resolution) {
return styles[feature.getGeometry().getType()];
};
var vectorSource = new ol.source.Vector();
new ol.format.GeoJSON().readObject({
'type': 'FeatureCollection',
'features': [
{
'type': 'Feature',
'geometry': {
'type': 'Point',
'coordinates': [0, 0]
}
},
{
'type': 'Feature',
'geometry': {
'type': 'LineString',
'coordinates': [[4e6, -2e6], [8e6, 2e6]]
}
},
{
'type': 'Feature',
'geometry': {
'type': 'LineString',
'coordinates': [[4e6, 2e6], [8e6, -2e6]]
}
},
{
'type': 'Feature',
'geometry': {
'type': 'Polygon',
'coordinates': [[[-5e6, -1e6], [-4e6, 1e6], [-3e6, -1e6]]]
}
},
{
'type': 'Feature',
'geometry': {
'type': 'MultiLineString',
'coordinates': [
[[-1e6, -7.5e5], [-1e6, 7.5e5]],
[[1e6, -7.5e5], [1e6, 7.5e5]],
[[-7.5e5, -1e6], [7.5e5, -1e6]],
[[-7.5e5, 1e6], [7.5e5, 1e6]]
var vectorSource = new ol.source.GeoJSON(
/** @type {ol.source.GeoJSONOptions} */ ({
object: {
'type': 'FeatureCollection',
'crs': {
'type': 'name',
'properties': {
'name': 'EPSG:3857'
}
},
'features': [
{
'type': 'Feature',
'geometry': {
'type': 'Point',
'coordinates': [0, 0]
}
},
{
'type': 'Feature',
'geometry': {
'type': 'LineString',
'coordinates': [[4e6, -2e6], [8e6, 2e6]]
}
},
{
'type': 'Feature',
'geometry': {
'type': 'LineString',
'coordinates': [[4e6, 2e6], [8e6, -2e6]]
}
},
{
'type': 'Feature',
'geometry': {
'type': 'Polygon',
'coordinates': [[[-5e6, -1e6], [-4e6, 1e6], [-3e6, -1e6]]]
}
},
{
'type': 'Feature',
'geometry': {
'type': 'MultiLineString',
'coordinates': [
[[-1e6, -7.5e5], [-1e6, 7.5e5]],
[[1e6, -7.5e5], [1e6, 7.5e5]],
[[-7.5e5, -1e6], [7.5e5, -1e6]],
[[-7.5e5, 1e6], [7.5e5, 1e6]]
]
}
},
{
'type': 'Feature',
'geometry': {
'type': 'MultiPolygon',
'coordinates': [
[[[-5e6, 6e6], [-5e6, 8e6], [-3e6, 8e6], [-3e6, 6e6]]],
[[[-2e6, 6e6], [-2e6, 8e6], [0e6, 8e6], [0e6, 6e6]]],
[[[1e6, 6e6], [1e6, 8e6], [3e6, 8e6], [3e6, 6e6]]]
]
}
}
]
}
},
{
'type': 'Feature',
'geometry': {
'type': 'MultiPolygon',
'coordinates': [
[[[-5e6, 6e6], [-5e6, 8e6], [-3e6, 8e6], [-3e6, 6e6]]],
[[[-2e6, 6e6], [-2e6, 8e6], [0e6, 8e6], [0e6, 6e6]]],
[[[1e6, 6e6], [1e6, 8e6], [3e6, 8e6], [3e6, 6e6]]]
]
}
}
]
}, vectorSource.addFeature, vectorSource);
}));
var vectorLayer = new ol.layer.Vector({
source: vectorSource,

View File

@@ -3,12 +3,11 @@ goog.require('ol.Overlay');
goog.require('ol.OverlayPositioning');
goog.require('ol.RendererHint');
goog.require('ol.View2D');
goog.require('ol.format.GeoJSON');
goog.require('ol.icon');
goog.require('ol.layer.Tile');
goog.require('ol.layer.Vector');
goog.require('ol.source.GeoJSON');
goog.require('ol.source.TileJSON');
goog.require('ol.source.Vector');
goog.require('ol.style.Style');
@@ -18,30 +17,28 @@ var raster = new ol.layer.Tile({
})
});
var vectorSource = new ol.source.Vector();
new ol.format.GeoJSON().readObject({
'type': 'FeatureCollection',
'features': [{
'type': 'Feature',
'properties': {
'name': 'Null Island',
'population': 4000,
'rainfall': 500
},
'geometry': {
'type': 'Point',
'coordinates': [0, 0]
}
}]
}, vectorSource.addFeature, vectorSource);
var styleArray = [new ol.style.Style({
image: ol.icon.renderIcon('data/icon.png')
})];
var vector = new ol.layer.Vector({
source: vectorSource,
source: new ol.source.GeoJSON(/** @type {ol.source.GeoJSONOptions} */ ({
object: {
'type': 'FeatureCollection',
'features': [{
'type': 'Feature',
'properties': {
'name': 'Null Island',
'population': 4000,
'rainfall': 500
},
'geometry': {
'type': 'Point',
'coordinates': [0, 0]
}
}]
}
})),
styleFunction: function(feature, resolution) {
return styleArray;
}

View File

@@ -1,31 +1,15 @@
goog.require('ol.Map');
goog.require('ol.RendererHint');
goog.require('ol.View2D');
goog.require('ol.format.GeoJSON');
goog.require('ol.layer.Tile');
goog.require('ol.layer.Vector');
goog.require('ol.proj');
goog.require('ol.source.GeoJSON');
goog.require('ol.source.MapQuestOpenAerial');
goog.require('ol.source.Vector');
goog.require('ol.style.Fill');
goog.require('ol.style.Stroke');
goog.require('ol.style.Style');
var map = new ol.Map({
layers: [
new ol.layer.Tile({
source: new ol.source.MapQuestOpenAerial()
})
],
renderer: ol.RendererHint.CANVAS,
target: 'map',
view: new ol.View2D({
center: [0, 0],
zoom: 2
})
});
var vectorSource = new ol.source.Vector();
var styleArray = [new ol.style.Style({
fill: new ol.style.Fill({
color: 'rgba(255, 255, 255, 0.6)'
@@ -36,23 +20,28 @@ var styleArray = [new ol.style.Style({
})
})];
var vectorLayer;
$.getJSON('data/countries.geojson', function(data) {
var format = new ol.format.GeoJSON();
var transformFn = ol.proj.getTransform('EPSG:4326', 'EPSG:3857');
format.readObject(data, function(feature) {
var geometry = feature.getGeometry();
geometry.transform(transformFn);
feature.setGeometry(geometry);
vectorSource.addFeature(feature);
});
vectorLayer = new ol.layer.Vector({
source: vectorSource,
styleFunction: function(feature, resolution) {
return styleArray;
}
});
map.getLayers().push(vectorLayer);
var vectorLayer = new ol.layer.Vector({
source: new ol.source.GeoJSON({
url: 'data/countries.geojson'
}),
styleFunction: function(feature, resolution) {
return styleArray;
}
});
var map = new ol.Map({
layers: [
new ol.layer.Tile({
source: new ol.source.MapQuestOpenAerial()
}),
vectorLayer
],
renderer: ol.RendererHint.CANVAS,
target: 'map',
view: new ol.View2D({
center: [0, 0],
zoom: 2
})
});
var highlight;

View File

@@ -247,7 +247,7 @@
/**
* @typedef {Object} ol.format.GeoJSONOptions
* @property {ol.proj.ProjectionLike} projection Projection.
* @property {ol.proj.ProjectionLike} defaultProjection Default projection.
*/
/**
@@ -479,11 +479,14 @@
/**
* @typedef {Object} ol.source.GeoJSONOptions
* @property {Array.<ol.Attribution>|undefined} attributions Attributions.
* @property {ol.proj.ProjectionLike} defaultProjection Default projection.
* @property {ol.Extent|undefined} extent Extent.
* @property {string|undefined} logo Logo.
* @property {GeoJSONObject|undefined} geoJSON Object.
* @property {GeoJSONObject|undefined} object Object.
* @property {ol.proj.ProjectionLike} projection Projection.
* @property {string|undefined} string String.
* @proprtty {string|undefined} url URL.
* @property {ol.proj.ProjectionLike} reprojectTo Re-project to.
* @property {string|undefined} text Text.
* @property {string|undefined} url URL.
*/
/**
@@ -617,6 +620,20 @@
* @property {ol.source.State|undefined} state State.
*/
/**
* @typedef {Object} ol.source.VectorFileOptions
* @property {Array.<ol.Attribution>|undefined} attributions Attributions.
* @property {Document|undefined} doc Document.
* @property {ol.Extent|undefined} extent Extent.
* @property {ol.format.Format} format Format.
* @property {string|undefined} logo Logo.
* @property {Node|undefined} node Node.
* @property {Object|undefined} object Object.
* @property {ol.proj.ProjectionLike} projection Projection.
* @property {string|undefined} text Text.
* @property {string|undefined} url URL.
*/
/**
* @typedef {Object} ol.source.WMTSOptions
* @property {Array.<ol.Attribution>|undefined} attributions Attributions.

View File

@@ -0,0 +1,2 @@
@exportProperty ol.format.Format.prototype.readProjection
@exportProperty ol.format.Format.prototype.readStyleFunction

View File

@@ -1,17 +1,101 @@
goog.provide('ol.format');
goog.provide('ol.format.Format');
goog.provide('ol.format.FormatType');
goog.require('goog.functions');
goog.require('ol.proj');
/**
* @param {function(Object, function(this: S, ol.Feature): T, S=)} reader
* Reader.
* @param {Object} object Object.
* @return {Array.<ol.Feature>}
* @enum {string}
*/
ol.format.FormatType = {
BINARY: 'binary',
JSON: 'json',
TEXT: 'text',
XML: 'xml'
};
/**
* @constructor
*/
ol.format.Format = function() {
};
/**
* @return {ol.format.FormatType} Format.
*/
ol.format.Format.prototype.getType = goog.abstractMethod;
/**
* @param {Document|Node|Object|string} source Source.
* @return {ol.Feature} Feature.
*/
ol.format.Format.prototype.readFeature = goog.abstractMethod;
/**
* @param {Document|Node|Object|string} source Source.
* @return {Array.<ol.Feature>} Features.
*/
ol.format.Format.prototype.readFeatures = goog.abstractMethod;
/**
* @param {Document|Node|Object|string} source Source.
* @param {function(this: S, ol.Feature, (Document|Node|Object|undefined)): T}
* callback Callback.
* @param {S=} opt_obj Scope.
* @template S,T
*/
ol.format.readAllFromObject = function(reader, object) {
var features = [];
reader(object, function(feature) {
features.push(feature);
});
return features;
ol.format.Format.prototype.readFeaturesAsync = goog.abstractMethod;
/**
* @param {Document|Node|Object|string} source Source.
* @return {ol.geom.Geometry} Geometry.
*/
ol.format.Format.prototype.readGeometry = goog.abstractMethod;
/**
* @param {Document|Node|Object|string} source Source.
* @return {ol.proj.Projection} Projection.
*/
ol.format.Format.prototype.readProjection = function(source) {
return ol.proj.get('EPSG:4326');
};
/**
* @param {Document|Node|Object|string} source Source.
* @return {function(ol.Feature, number): Array.<ol.style.Style>} Style
* function.
*/
ol.format.Format.prototype.readStyleFunction = function(source) {
return goog.functions.NULL;
};
/**
* @param {ol.Feature} feature Feature.
* @return {Node|Object|string} Result.
*/
ol.format.Format.prototype.writeFeature = goog.abstractMethod;
/**
* @param {Array.<ol.Feature>} features Features.
* @return {Node|Object|string} Result.
*/
ol.format.Format.prototype.writeFeatures = goog.abstractMethod;
/**
* @param {ol.geom.Geometry} geometry Geometry.
* @return {Node|Object|string} Node.
*/
ol.format.Format.prototype.writeGeometry = goog.abstractMethod;

View File

@@ -1,3 +0,0 @@
@exportClass ol.format.GeoJSON ol.format.GeoJSONOptions
@exportProperty ol.format.GeoJSON.prototype.readObject
@exportProperty ol.format.GeoJSON.prototype.readString

View File

@@ -1,171 +0,0 @@
// FIXME coordinate order
// FIXME reprojection
// FIXME support other geometry types
goog.provide('ol.format.GeoJSON');
goog.require('goog.asserts');
goog.require('goog.json');
goog.require('ol.Feature');
goog.require('ol.format.IReader');
goog.require('ol.geom.LineString');
goog.require('ol.geom.MultiLineString');
goog.require('ol.geom.MultiPolygon');
goog.require('ol.geom.Point');
goog.require('ol.geom.Polygon');
/**
* @constructor
* @implements {ol.format.IReader}
* @param {ol.format.GeoJSONOptions=} opt_options Options.
*/
ol.format.GeoJSON = function(opt_options) {
};
/**
* @param {GeoJSONGeometry} geometry Geometry.
* @private
* @return {ol.geom.Point} Point.
*/
ol.format.GeoJSON.readPointGeometry_ = function(geometry) {
goog.asserts.assert(geometry.type == 'Point');
return new ol.geom.Point(geometry.coordinates);
};
/**
* @param {GeoJSONGeometry} geometry Geometry.
* @private
* @return {ol.geom.LineString} LineString.
*/
ol.format.GeoJSON.readLineStringGeometry_ = function(geometry) {
goog.asserts.assert(geometry.type == 'LineString');
return new ol.geom.LineString(geometry.coordinates);
};
/**
* @param {GeoJSONGeometry} geometry Geometry.
* @private
* @return {ol.geom.MultiLineString} MultiLineString.
*/
ol.format.GeoJSON.readMultiLineStringGeometry_ = function(geometry) {
goog.asserts.assert(geometry.type == 'MultiLineString');
return new ol.geom.MultiLineString(geometry.coordinates);
};
/**
* @param {GeoJSONGeometry} geometry Geometry.
* @private
* @return {ol.geom.MultiPolygon} MultiPolygon.
*/
ol.format.GeoJSON.readMultiPolygonGeometry_ = function(geometry) {
goog.asserts.assert(geometry.type == 'MultiPolygon');
return new ol.geom.MultiPolygon(geometry.coordinates);
};
/**
* @param {GeoJSONGeometry} geometry Geometry.
* @private
* @return {ol.geom.Polygon} Polygon.
*/
ol.format.GeoJSON.readPolygonGeometry_ = function(geometry) {
goog.asserts.assert(geometry.type == 'Polygon');
return new ol.geom.Polygon(geometry.coordinates);
};
/**
* @param {GeoJSONObject} object Object.
* @param {function(this: S, ol.Feature): T} callback Callback.
* @param {S=} opt_obj Scope.
* @private
* @return {T} Callback result.
* @template S,T
*/
ol.format.GeoJSON.readFeature_ = function(object, callback, opt_obj) {
goog.asserts.assert(object.type == 'Feature');
var feature = /** @type {GeoJSONFeature} */ (object);
var geometryReader =
ol.format.GeoJSON.GEOMETRY_READERS_[feature.geometry.type];
goog.asserts.assert(goog.isDef(geometryReader));
var geometry = geometryReader(feature.geometry);
var f = new ol.Feature(geometry);
f.setId(feature.id);
if (goog.isDef(feature.properties)) {
f.setValues(feature.properties);
}
return callback.call(opt_obj, f);
};
/**
* @param {GeoJSONObject} object Object.
* @param {function(this: S, ol.Feature): T} callback Callback.
* @param {S=} opt_obj Scope.
* @private
* @return {T|undefined} Callback result.
* @template S,T
*/
ol.format.GeoJSON.readFeatureCollection_ = function(object, callback, opt_obj) {
goog.asserts.assert(object.type == 'FeatureCollection');
var featureCollection = /** @type {GeoJSONFeatureCollection} */ (object);
var features = featureCollection.features;
var i, ii;
for (i = 0, ii = features.length; i < ii; ++i) {
var result = ol.format.GeoJSON.readFeature_(features[i], callback, opt_obj);
if (result) {
return result;
}
}
return undefined;
};
/**
* @inheritDoc
*/
ol.format.GeoJSON.prototype.readObject = function(object, callback, opt_obj) {
var geoJSONObject = /** @type {GeoJSONObject} */ (object);
var objectReader = ol.format.GeoJSON.OBJECT_READERS_[geoJSONObject.type];
goog.asserts.assert(goog.isDef(objectReader));
return objectReader(geoJSONObject, callback, opt_obj);
};
/**
* @inheritDoc
*/
ol.format.GeoJSON.prototype.readString = function(string, callback, opt_obj) {
return this.readObject(goog.json.parse(string), callback, opt_obj);
};
/**
* @const
* @private
* @type {Object.<string, function(GeoJSONGeometry): ol.geom.Geometry>}
*/
ol.format.GeoJSON.GEOMETRY_READERS_ = {
'Point': ol.format.GeoJSON.readPointGeometry_,
'LineString': ol.format.GeoJSON.readLineStringGeometry_,
'Polygon': ol.format.GeoJSON.readPolygonGeometry_,
'MultiLineString': ol.format.GeoJSON.readMultiLineStringGeometry_,
'MultiPolygon': ol.format.GeoJSON.readMultiPolygonGeometry_
};
/**
* @const
* @private
* @type {Object.<string, function(GeoJSONObject, function(ol.Feature): *, *): *>}
*/
ol.format.GeoJSON.OBJECT_READERS_ = {
'Feature': ol.format.GeoJSON.readFeature_,
'FeatureCollection': ol.format.GeoJSON.readFeatureCollection_
};

View File

@@ -0,0 +1 @@
@exportClass ol.format.GeoJSON ol.format.GeoJSONOptions

View File

@@ -0,0 +1,365 @@
// FIXME coordinate order
// FIXME reprojection
// FIXME GeometryCollection
goog.provide('ol.format.GeoJSON');
goog.require('goog.asserts');
goog.require('goog.object');
goog.require('ol.Feature');
goog.require('ol.format.JSON');
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');
goog.require('ol.proj');
/**
* @constructor
* @extends {ol.format.JSON}
* @param {ol.format.GeoJSONOptions=} opt_options Options.
*/
ol.format.GeoJSON = function(opt_options) {
var options = goog.isDef(opt_options) ? opt_options : {};
goog.base(this);
/**
* @private
* @type {ol.proj.Projection}
*/
this.defaultProjection_ = ol.proj.get(options.defaultProjection ?
options.defaultProjection : 'EPSG:4326');
};
goog.inherits(ol.format.GeoJSON, ol.format.JSON);
/**
* @param {GeoJSONGeometry} object Object.
* @private
* @return {ol.geom.Geometry} Geometry.
*/
ol.format.GeoJSON.readGeometry_ = function(object) {
var geometryReader = ol.format.GeoJSON.GEOMETRY_READERS_[object.type];
goog.asserts.assert(goog.isDef(geometryReader));
return geometryReader(object);
};
/**
* @param {GeoJSONGeometry} object Object.
* @private
* @return {ol.geom.Point} Point.
*/
ol.format.GeoJSON.readPointGeometry_ = function(object) {
goog.asserts.assert(object.type == 'Point');
return new ol.geom.Point(object.coordinates);
};
/**
* @param {GeoJSONGeometry} object Object.
* @private
* @return {ol.geom.LineString} LineString.
*/
ol.format.GeoJSON.readLineStringGeometry_ = function(object) {
goog.asserts.assert(object.type == 'LineString');
return new ol.geom.LineString(object.coordinates);
};
/**
* @param {GeoJSONGeometry} object Object.
* @private
* @return {ol.geom.MultiLineString} MultiLineString.
*/
ol.format.GeoJSON.readMultiLineStringGeometry_ = function(object) {
goog.asserts.assert(object.type == 'MultiLineString');
return new ol.geom.MultiLineString(object.coordinates);
};
/**
* @param {GeoJSONGeometry} object Object.
* @private
* @return {ol.geom.MultiPoint} MultiPoint.
*/
ol.format.GeoJSON.readMultiPointGeometry_ = function(object) {
goog.asserts.assert(object.type == 'MultiPoint');
return new ol.geom.MultiPoint(object.coordinates);
};
/**
* @param {GeoJSONGeometry} object Object.
* @private
* @return {ol.geom.MultiPolygon} MultiPolygon.
*/
ol.format.GeoJSON.readMultiPolygonGeometry_ = function(object) {
goog.asserts.assert(object.type == 'MultiPolygon');
return new ol.geom.MultiPolygon(object.coordinates);
};
/**
* @param {GeoJSONGeometry} object Object.
* @private
* @return {ol.geom.Polygon} Polygon.
*/
ol.format.GeoJSON.readPolygonGeometry_ = function(object) {
goog.asserts.assert(object.type == 'Polygon');
return new ol.geom.Polygon(object.coordinates);
};
/**
* @param {ol.geom.Geometry} geometry Geometry.
* @private
* @return {GeoJSONGeometry} GeoJSON geometry.
*/
ol.format.GeoJSON.writeGeometry_ = function(geometry) {
var geometryWriter = ol.format.GeoJSON.GEOMETRY_WRITERS_[geometry.getType()];
goog.asserts.assert(goog.isDef(geometryWriter));
return geometryWriter(geometry);
};
/**
* @param {ol.geom.Geometry} geometry Geometry.
* @private
* @return {GeoJSONGeometry} GeoJSON geometry.
*/
ol.format.GeoJSON.writeLineStringGeometry_ = function(geometry) {
goog.asserts.assertInstanceof(geometry, ol.geom.LineString);
return /** @type {GeoJSONGeometry} */ ({
'type': 'LineString',
'coordinates': geometry.getCoordinates()
});
};
/**
* @param {ol.geom.Geometry} geometry Geometry.
* @private
* @return {GeoJSONGeometry} GeoJSON geometry.
*/
ol.format.GeoJSON.writeMultiLineStringGeometry_ = function(geometry) {
goog.asserts.assertInstanceof(geometry, ol.geom.MultiLineString);
goog.asserts.assert(
geometry.getType() == ol.geom.GeometryType.MULTI_LINE_STRING);
return /** @type {GeoJSONGeometry} */ ({
'type': 'MultiLineString',
'coordinates': geometry.getCoordinates()
});
};
/**
* @param {ol.geom.Geometry} geometry Geometry.
* @private
* @return {GeoJSONGeometry} GeoJSON geometry.
*/
ol.format.GeoJSON.writeMultiPointGeometry_ = function(geometry) {
goog.asserts.assertInstanceof(geometry, ol.geom.MultiPoint);
return /** @type {GeoJSONGeometry} */ ({
'type': 'MultiPoint',
'coordinates': geometry.getCoordinates()
});
};
/**
* @param {ol.geom.Geometry} geometry Geometry.
* @private
* @return {GeoJSONGeometry} GeoJSON geometry.
*/
ol.format.GeoJSON.writeMultiPolygonGeometry_ = function(geometry) {
goog.asserts.assertInstanceof(geometry, ol.geom.MultiPolygon);
return /** @type {GeoJSONGeometry} */ ({
'type': 'MultiPolygon',
'coordinates': geometry.getCoordinates()
});
};
/**
* @param {ol.geom.Geometry} geometry Geometry.
* @private
* @return {GeoJSONGeometry} GeoJSON geometry.
*/
ol.format.GeoJSON.writePointGeometry_ = function(geometry) {
goog.asserts.assertInstanceof(geometry, ol.geom.Point);
return /** @type {GeoJSONGeometry} */ ({
'type': 'Point',
'coordinates': geometry.getCoordinates()
});
};
/**
* @param {ol.geom.Geometry} geometry Geometry.
* @private
* @return {GeoJSONGeometry} GeoJSON geometry.
*/
ol.format.GeoJSON.writePolygonGeometry_ = function(geometry) {
goog.asserts.assertInstanceof(geometry, ol.geom.Polygon);
return /** @type {GeoJSONGeometry} */ ({
'type': 'Polygon',
'coordinates': geometry.getCoordinates()
});
};
/**
* @const
* @private
* @type {Object.<string, function(GeoJSONGeometry): ol.geom.Geometry>}
*/
ol.format.GeoJSON.GEOMETRY_READERS_ = {
'Point': ol.format.GeoJSON.readPointGeometry_,
'LineString': ol.format.GeoJSON.readLineStringGeometry_,
'Polygon': ol.format.GeoJSON.readPolygonGeometry_,
'MultiPoint': ol.format.GeoJSON.readMultiPointGeometry_,
'MultiLineString': ol.format.GeoJSON.readMultiLineStringGeometry_,
'MultiPolygon': ol.format.GeoJSON.readMultiPolygonGeometry_
};
/**
* @const
* @private
* @type {Object.<string, function(ol.geom.Geometry): GeoJSONGeometry>}
*/
ol.format.GeoJSON.GEOMETRY_WRITERS_ = {
'Point': ol.format.GeoJSON.writePointGeometry_,
'LineString': ol.format.GeoJSON.writeLineStringGeometry_,
'Polygon': ol.format.GeoJSON.writePolygonGeometry_,
'MultiPoint': ol.format.GeoJSON.writeMultiPointGeometry_,
'MultiLineString': ol.format.GeoJSON.writeMultiLineStringGeometry_,
'MultiPolygon': ol.format.GeoJSON.writeMultiPolygonGeometry_
};
/**
* @inheritDoc
*/
ol.format.GeoJSON.prototype.readFeatureFromObject = function(object) {
var geoJSONFeature = /** @type {GeoJSONFeature} */ (object);
goog.asserts.assert(geoJSONFeature.type == 'Feature');
var geometry = ol.format.GeoJSON.readGeometry_(geoJSONFeature.geometry);
var feature = new ol.Feature(geometry);
if (goog.isDef(geoJSONFeature.id)) {
feature.setId(geoJSONFeature.id);
}
if (goog.isDef(geoJSONFeature.properties)) {
feature.setValues(geoJSONFeature.properties);
}
return feature;
};
/**
* @inheritDoc
*/
ol.format.GeoJSON.prototype.readFeaturesFromObject = function(object) {
var geoJSONObject = /** @type {GeoJSONObject} */ (object);
if (geoJSONObject.type == 'Feature') {
return [this.readFeatureFromObject(object)];
} else if (geoJSONObject.type == 'FeatureCollection') {
var geoJSONFeatureCollection = /** @type {GeoJSONFeatureCollection} */
(object);
/** @type {Array.<ol.Feature>} */
var features = [];
var geoJSONFeatures = geoJSONFeatureCollection.features;
var i, ii;
for (i = 0, ii = geoJSONFeatures.length; i < ii; ++i) {
features.push(this.readFeatureFromObject(geoJSONFeatures[i]));
}
return features;
} else {
goog.asserts.fail();
return null;
}
};
/**
* @inheritDoc
*/
ol.format.GeoJSON.prototype.readGeometryFromObject = function(object) {
return ol.format.GeoJSON.readGeometry_(
/** @type {GeoJSONGeometry} */ (object));
};
/**
* @inheritDoc
*/
ol.format.GeoJSON.prototype.readProjection = function(object) {
var geoJSONObject = /** @type {GeoJSONObject} */ (object);
var crs = geoJSONObject.crs;
if (goog.isDefAndNotNull(crs)) {
if (crs.type == 'name') {
return ol.proj.get(crs.properties.name);
} else {
goog.asserts.fail();
return null;
}
} else {
return this.defaultProjection_;
}
};
/**
* @inheritDoc
*/
ol.format.GeoJSON.prototype.writeFeatureObject = function(feature) {
var object = {
'type': 'Feature'
};
var id = feature.getId();
if (goog.isDefAndNotNull(id)) {
goog.object.set(object, 'id', id);
}
var geometry = feature.getGeometry();
if (goog.isDefAndNotNull(geometry)) {
goog.object.set(
object, 'geometry', ol.format.GeoJSON.writeGeometry_(geometry));
}
var properties = feature.getProperties();
goog.object.remove(properties, 'geometry');
if (!goog.object.isEmpty(properties)) {
goog.object.set(object, 'properties', properties);
}
return object;
};
/**
* @inheritDoc
*/
ol.format.GeoJSON.prototype.writeFeaturesObject = function(features) {
var objects = [];
var i, ii;
for (i = 0, ii = features.length; i < ii; ++i) {
objects.push(this.writeFeatureObject(features[i]));
}
return /** @type {GeoJSONFeatureCollection} */ ({
'type': 'FeatureCollection',
'features': objects
});
};
/**
* @inheritDoc
*/
ol.format.GeoJSON.prototype.writeGeometryObject =
ol.format.GeoJSON.writeGeometry_;

View File

@@ -1,34 +0,0 @@
// FIXME add XML
// FIXME add IWriter
goog.provide('ol.format.IReader');
/**
* @interface
*/
ol.format.IReader = function() {
};
/**
* @param {Object} object Object.
* @param {function(this: S, ol.Feature): T} callback Callback.
* @param {S=} opt_obj Scope.
* @return {T|undefined} Callback result.
* @template S,T
*/
ol.format.IReader.prototype.readObject = function(object, callback, opt_obj) {
};
/**
* @param {string} string String.
* @param {function(this: S, ol.Feature): T} callback Callback.
* @param {S=} opt_obj Scope.
* @return {T|undefined} Callback result.
* @template S,T
*/
ol.format.IReader.prototype.readString = function(string, callback, opt_obj) {
};

View File

@@ -0,0 +1,8 @@
@exportProperty ol.format.JSON.prototype.readFeature
@exportProperty ol.format.JSON.prototype.readFeatures
@exportProperty ol.format.JSON.prototype.readFeaturesAsync
@exportProperty ol.format.JSON.prototype.readGeometry
@exportProperty ol.format.JSON.prototype.readProjection
@exportProperty ol.format.JSON.prototype.writeFeature
@exportProperty ol.format.JSON.prototype.writeFeatures
@exportProperty ol.format.JSON.prototype.writeGeometry

155
src/ol/format/jsonformat.js Normal file
View File

@@ -0,0 +1,155 @@
goog.provide('ol.format.JSON');
goog.require('goog.asserts');
goog.require('goog.json');
goog.require('ol.format.Format');
goog.require('ol.format.FormatType');
/**
* @constructor
* @extends {ol.format.Format}
*/
ol.format.JSON = function() {
goog.base(this);
};
goog.inherits(ol.format.JSON, ol.format.Format);
/**
* @param {Document|Node|Object|string} source Source.
* @private
* @return {Object} Object.
*/
ol.format.JSON.prototype.getObject_ = function(source) {
if (goog.isObject(source)) {
return source;
} else if (goog.isString(source)) {
var object = goog.json.parse(source);
return goog.isDef(object) ? object : null;
} else {
goog.asserts.fail();
return null;
}
};
/**
* @inheritDoc
*/
ol.format.JSON.prototype.getType = function() {
return ol.format.FormatType.JSON;
};
/**
* @inheritDoc
*/
ol.format.JSON.prototype.readFeature = function(source) {
return this.readFeatureFromObject(this.getObject_(source));
};
/**
* @inheritDoc
*/
ol.format.JSON.prototype.readFeatures = function(source) {
return this.readFeaturesFromObject(this.getObject_(source));
};
/**
* @param {Object} object Object.
* @protected
* @return {ol.Feature} Feature.
*/
ol.format.JSON.prototype.readFeatureFromObject = goog.abstractMethod;
/**
* @param {Object} object Object.
* @protected
* @return {Array.<ol.Feature>} Features.
*/
ol.format.JSON.prototype.readFeaturesFromObject = goog.abstractMethod;
/**
* @inheritDoc
*/
ol.format.JSON.prototype.readGeometry = function(source) {
return this.readGeometryFromObject(this.getObject_(source));
};
/**
* @param {Object} object Object.
* @protected
* @return {ol.geom.Geometry} Geometry.
*/
ol.format.JSON.prototype.readGeometryFromObject = goog.abstractMethod;
/**
* @inheritDoc
*/
ol.format.JSON.prototype.readProjection = function(source) {
return this.readProjectionFromObject(this.getObject_(source));
};
/**
* @param {Object} object Object.
* @protected
* @return {ol.proj.Projection} Projection.
*/
ol.format.JSON.prototype.readProjectionFromObject = goog.abstractMethod;
/**
* @inheritDoc
*/
ol.format.JSON.prototype.writeFeature = function(feature) {
return this.writeFeatureObject(feature);
};
/**
* @param {ol.Feature} feature Feature.
* @protected
* @return {Object} Object.
*/
ol.format.JSON.prototype.writeFeatureObject = goog.abstractMethod;
/**
* @inheritDoc
*/
ol.format.JSON.prototype.writeFeatures = function(features) {
return this.writeFeaturesObject(features);
};
/**
* @param {Array.<ol.Feature>} features Features.
* @protected
* @return {Object} Object.
*/
ol.format.JSON.prototype.writeFeaturesObject = goog.abstractMethod;
/**
* @inheritDoc
*/
ol.format.JSON.prototype.writeGeometry = function(geometry) {
return this.writeGeometryObject(geometry);
};
/**
* @param {ol.geom.Geometry} geometry Geometry.
* @protected
* @return {Object} Object.
*/
ol.format.JSON.prototype.writeGeometryObject = goog.abstractMethod;

View File

@@ -0,0 +1,8 @@
@exportProperty ol.format.Text.prototype.readFeature
@exportProperty ol.format.Text.prototype.readFeatures
@exportProperty ol.format.Text.prototype.readFeaturesAsync
@exportProperty ol.format.Text.prototype.readGeometry
@exportProperty ol.format.Text.prototype.readProjection
@exportProperty ol.format.Text.prototype.writeFeature
@exportProperty ol.format.Text.prototype.writeFeatures
@exportProperty ol.format.Text.prototype.writeGeometry

154
src/ol/format/textformat.js Normal file
View File

@@ -0,0 +1,154 @@
goog.provide('ol.format.Text');
goog.require('goog.asserts');
goog.require('ol.format.Format');
goog.require('ol.format.FormatType');
goog.require('ol.proj');
/**
* @constructor
* @extends {ol.format.Format}
*/
ol.format.Text = function() {
goog.base(this);
};
goog.inherits(ol.format.Text, ol.format.Format);
/**
* @param {Document|Node|Object|string} source Source.
* @private
* @return {string} Text.
*/
ol.format.Text.prototype.getText_ = function(source) {
if (goog.isString(source)) {
return source;
} else {
goog.asserts.fail();
return '';
}
};
/**
* @inheritDoc
*/
ol.format.Text.prototype.getType = function() {
return ol.format.FormatType.TEXT;
};
/**
* @inheritDoc
*/
ol.format.Text.prototype.readFeature = function(source) {
return this.readFeatureFromText(this.getText_(source));
};
/**
* @param {string} text Text.
* @protected
* @return {ol.Feature} Feature.
*/
ol.format.Text.prototype.readFeatureFromText = goog.abstractMethod;
/**
* @inheritDoc
*/
ol.format.Text.prototype.readFeatures = function(source) {
return this.readFeaturesFromText(this.getText_(source));
};
/**
* @param {string} text Text.
* @protected
* @return {Array.<ol.Feature>} Features.
*/
ol.format.Text.prototype.readFeaturesFromText = goog.abstractMethod;
/**
* @inheritDoc
*/
ol.format.Text.prototype.readGeometry = function(source) {
return this.readGeometryFromText(this.getText_(source));
};
/**
* @param {string} text Text.
* @protected
* @return {ol.geom.Geometry} Geometry.
*/
ol.format.Text.prototype.readGeometryFromText = goog.abstractMethod;
/**
* @inheritDoc
*/
ol.format.Text.prototype.readProjection = function(source) {
return this.readProjectionFromText(this.getText_(source));
};
/**
* @param {string} text Text.
* @protected
* @return {ol.proj.Projection} Projection.
*/
ol.format.Text.prototype.readProjectionFromText = function(text) {
return ol.proj.get('EPSG:4326');
};
/**
* @inheritDoc
*/
ol.format.Text.prototype.writeFeature = function(feature) {
return this.writeFeatureText(feature);
};
/**
* @param {ol.Feature} feature Features.
* @protected
* @return {string} Text.
*/
ol.format.Text.prototype.writeFeatureText = goog.abstractMethod;
/**
* @inheritDoc
*/
ol.format.Text.prototype.writeFeatures = function(features) {
return this.writeFeaturesText(features);
};
/**
* @param {Array.<ol.Feature>} features Features.
* @protected
* @return {string} Text.
*/
ol.format.Text.prototype.writeFeaturesText = goog.abstractMethod;
/**
* @inheritDoc
*/
ol.format.Text.prototype.writeGeometry = function(geometry) {
return this.writeGeometryText(geometry);
};
/**
* @param {ol.geom.Geometry} geometry Geometry.
* @protected
* @return {string} Text.
*/
ol.format.Text.prototype.writeGeometryText = goog.abstractMethod;

View File

@@ -0,0 +1,8 @@
@exportProperty ol.format.XML.prototype.readFeature
@exportProperty ol.format.XML.prototype.readFeatures
@exportProperty ol.format.XML.prototype.readFeaturesAsync
@exportProperty ol.format.XML.prototype.readGeometry
@exportProperty ol.format.XML.prototype.readProjection
@exportProperty ol.format.XML.prototype.writeFeature
@exportProperty ol.format.XML.prototype.writeFeatures
@exportProperty ol.format.XML.prototype.writeGeometry

209
src/ol/format/xmlformat.js Normal file
View File

@@ -0,0 +1,209 @@
goog.provide('ol.format.XML');
goog.require('goog.asserts');
goog.require('goog.dom.xml');
goog.require('ol.format.Format');
goog.require('ol.format.FormatType');
goog.require('ol.proj');
/**
* @constructor
* @extends {ol.format.Format}
*/
ol.format.XML = function() {
goog.base(this);
};
goog.inherits(ol.format.XML, ol.format.Format);
/**
* @param {Document|Node|Object|string} source Source.
* @private
* @return {Document} Document.
*/
ol.format.XML.prototype.getDocument_ = function(source) {
if (source instanceof Document) {
return source;
} else if (goog.isString(source)) {
return goog.dom.xml.loadXml(source);
} else {
goog.asserts.fail();
return null;
}
};
/**
* @param {Document|Node|Object|string} source Source.
* @private
* @return {Document|Node} Document.
*/
ol.format.XML.prototype.getDocumentOrNode_ = function(source) {
if (source instanceof Document) {
return source;
} else if (source instanceof Node) {
return source;
} else if (goog.isString(source)) {
return goog.dom.xml.loadXml(source);
} else {
goog.asserts.fail();
return null;
}
};
/**
* @param {Document|Node|Object|string} source Source.
* @private
* @return {Node} Node.
*/
ol.format.XML.prototype.getNode_ = function(source) {
if (source instanceof Node) {
return source;
} else {
goog.asserts.fail();
return null;
}
};
/**
* @inheritDoc
*/
ol.format.XML.prototype.getType = function() {
return ol.format.FormatType.XML;
};
/**
* @inheritDoc
*/
ol.format.XML.prototype.readFeature = function(source) {
return this.readFeatureFromNode(this.getNode_(source));
};
/**
* @param {Node} node Node.
* @return {ol.Feature} Feature.
*/
ol.format.XML.prototype.readFeatureFromNode = goog.abstractMethod;
/**
* @inheritDoc
*/
ol.format.XML.prototype.readFeatures = function(source) {
var documentOrNode = this.getDocumentOrNode_(source);
if (documentOrNode instanceof Document) {
return this.readFeaturesFromDocument(documentOrNode);
} else if (documentOrNode instanceof Node) {
return this.readFeaturesFromNode(documentOrNode);
} else {
goog.asserts.fail();
return null;
}
};
/**
* @param {Document} doc Document.
* @protected
* @return {Array.<ol.Feature>} Features.
*/
ol.format.XML.prototype.readFeaturesFromDocument = function(doc) {
goog.asserts.assert(doc.childNodes.length == 1);
return this.readFeaturesFromNode(doc.firstChild);
};
/**
* @param {Node} node Node.
* @protected
* @return {Array.<ol.Feature>} Features.
*/
ol.format.XML.prototype.readFeaturesFromNode = goog.abstractMethod;
/**
* @inheritDoc
*/
ol.format.XML.prototype.readGeometry = function(source) {
return this.readGeometryFromNode(this.getNode_(source));
};
/**
* @param {Node} node Node.
* @protected
* @return {ol.geom.Geometry} Geometry.
*/
ol.format.XML.prototype.readGeometryFromNode = goog.abstractMethod;
/**
* @inheritDoc
*/
ol.format.XML.prototype.readProjection = function(source) {
return this.readProjectionFromNode(this.getNode_(source));
};
/**
* @param {Node} node Node.
* @protected
* @return {ol.proj.Projection} Projection.
*/
ol.format.XML.prototype.readProjectionFromNode = function(node) {
return ol.proj.get('EPSG:4326');
};
/**
* @inheritDoc
*/
ol.format.XML.prototype.writeFeature = function(feature) {
return this.writeFeatureNode(feature);
};
/**
* @param {ol.Feature} feature Feature.
* @protected
* @return {Node} Node.
*/
ol.format.XML.prototype.writeFeatureNode = goog.abstractMethod;
/**
* @inheritDoc
*/
ol.format.XML.prototype.writeFeatures = function(features) {
return this.writeFeaturesNode(features);
};
/**
* @param {Array.<ol.Feature>} features Features.
* @protected
* @return {Node} Node.
*/
ol.format.XML.prototype.writeFeaturesNode = goog.abstractMethod;
/**
* @inheritDoc
*/
ol.format.XML.prototype.writeGeometry = function(geometry) {
return this.writeGeometryNode(geometry);
};
/**
* @param {ol.geom.Geometry} geometry Geometry.
* @protected
* @return {Node} Node.
*/
ol.format.XML.prototype.writeGeometryNode = goog.abstractMethod;

View File

@@ -0,0 +1 @@
@exportClass ol.source.GeoJSON ol.source.GeoJSONOptions

View File

@@ -0,0 +1,32 @@
goog.provide('ol.source.GeoJSON');
goog.require('ol.format.GeoJSON');
goog.require('ol.source.VectorFile');
/**
* @constructor
* @extends {ol.source.VectorFile}
* @param {ol.source.GeoJSONOptions=} opt_options Options.
*/
ol.source.GeoJSON = function(opt_options) {
var options = goog.isDef(opt_options) ? opt_options : {};
goog.base(this, {
attributions: options.attributions,
extent: options.extent,
format: new ol.format.GeoJSON({
defaultProjection: options.defaultProjection
}),
logo: options.logo,
object: options.object,
projection: options.projection,
reprojectTo: options.reprojectTo,
text: options.text,
url: options.url
});
};
goog.inherits(ol.source.GeoJSON, ol.source.VectorFile);

View File

@@ -0,0 +1 @@
@exportClass ol.source.VectorFile ol.source.VectorFileOptions

View File

@@ -0,0 +1,125 @@
// FIXME remove reprojectTo
goog.provide('ol.source.VectorFile');
goog.require('goog.asserts');
goog.require('goog.net.XhrIo');
goog.require('ol.format.FormatType');
goog.require('ol.proj');
goog.require('ol.source.State');
goog.require('ol.source.Vector');
/**
* @constructor
* @extends {ol.source.Vector}
* @param {ol.source.VectorFileOptions=} opt_options Options.
*/
ol.source.VectorFile = function(opt_options) {
var options = goog.isDef(opt_options) ? opt_options : {};
goog.base(this, {
attributions: options.attributions,
extent: options.extent,
logo: options.logo,
projection: options.projection
});
/**
* @type {ol.format.Format}
* @protected
*/
this.format = options.format;
/**
* @type {ol.proj.Projection}
* @private
*/
this.reprojectTo_ = goog.isDef(options.reprojectTo) ?
ol.proj.get(options.reprojectTo) : ol.proj.get('EPSG:3857');
if (goog.isDef(options.doc)) {
this.readFeatures_(options.doc);
}
if (goog.isDef(options.node)) {
this.readFeatures_(options.node);
}
if (goog.isDef(options.object)) {
this.readFeatures_(options.object);
}
if (goog.isDef(options.text)) {
this.readFeatures_(options.text);
}
if (goog.isDef(options.url)) {
this.setState(ol.source.State.LOADING);
goog.net.XhrIo.send(options.url, goog.bind(this.handleXhrIo_, this));
}
};
goog.inherits(ol.source.VectorFile, ol.source.Vector);
/**
* @param {Event} event Event.
* @private
*/
ol.source.VectorFile.prototype.handleXhrIo_ = function(event) {
var xhrIo = /** @type {goog.net.XhrIo} */ (event.target);
if (xhrIo.isSuccess()) {
var type = this.format.getType();
/** @type {Document|Node|Object|string|undefined} */
var source;
if (type == ol.format.FormatType.BINARY) {
// FIXME
} else if (type == ol.format.FormatType.JSON) {
source = xhrIo.getResponseJson();
} else if (type == ol.format.FormatType.TEXT) {
source = xhrIo.getResponseText();
} else if (type == ol.format.FormatType.XML) {
source = xhrIo.getResponseXml();
} else {
goog.asserts.fail();
}
if (goog.isDef(source)) {
this.readFeatures_(source);
} else {
goog.asserts.fail();
this.setState(ol.source.State.ERROR);
}
} else {
this.setState(ol.source.State.ERROR);
}
};
/**
* @param {Document|Node|Object|string} source Source.
* @private
*/
ol.source.VectorFile.prototype.readFeatures_ = function(source) {
var format = this.format;
var features = format.readFeatures(source);
var featureProjection = format.readProjection(source);
var transform;
if (!ol.proj.equivalent(featureProjection, this.reprojectTo_)) {
transform = ol.proj.getTransform(featureProjection, this.reprojectTo_);
} else {
transform = null;
}
var i, ii;
for (i = 0, ii = features.length; i < ii; ++i) {
var feature = features[i];
var geometry = feature.getGeometry();
if (!goog.isNull(geometry) && !goog.isNull(transform)) {
geometry.transform(transform);
}
this.addFeature(feature);
}
this.setState(ol.source.State.READY);
};

File diff suppressed because one or more lines are too long

View File

@@ -3,6 +3,11 @@ goog.provide('ol.test.reader.GeoJSON');
describe('ol.format.GeoJSON', function() {
var format;
beforeEach(function() {
format = new ol.format.GeoJSON();
});
var pointGeoJSON = {
'type': 'Feature',
'geometry': {
@@ -47,14 +52,74 @@ describe('ol.format.GeoJSON', function() {
'features': [pointGeoJSON, lineStringGeoJSON, polygonGeoJSON]
};
var format = new ol.format.GeoJSON();
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('readObject', function() {
describe('#readFeature', function() {
it('can read a single point feature', function() {
var feature = format.readObject(pointGeoJSON, function(f) {
return f;
});
var feature = format.readFeature(pointGeoJSON);
expect(feature).to.be.an(ol.Feature);
var geometry = feature.getGeometry();
expect(geometry).to.be.an(ol.geom.Point);
@@ -63,10 +128,7 @@ describe('ol.format.GeoJSON', function() {
});
it('can read a single line string feature', function() {
var feature = format.readObject(lineStringGeoJSON,
function(f) {
return f;
});
var feature = format.readFeature(lineStringGeoJSON);
expect(feature).to.be.an(ol.Feature);
var geometry = feature.getGeometry();
expect(geometry).to.be.an(ol.geom.LineString);
@@ -77,9 +139,7 @@ describe('ol.format.GeoJSON', function() {
});
it('can read a single polygon feature', function() {
var feature = format.readObject(polygonGeoJSON, function(f) {
return f;
});
var feature = format.readFeature(polygonGeoJSON);
expect(feature).to.be.an(ol.Feature);
var geometry = feature.getGeometry();
expect(geometry).to.be.an(ol.geom.Polygon);
@@ -91,10 +151,7 @@ describe('ol.format.GeoJSON', function() {
});
it('can read a feature collection', function() {
var features = [];
format.readObject(featureCollectionGeoJSON, function(f) {
features.push(f);
});
var features = format.readFeatures(featureCollectionGeoJSON);
expect(features).to.have.length(3);
expect(features[0].getGeometry()).to.be.an(ol.geom.Point);
expect(features[1].getGeometry()).to.be.an(ol.geom.LineString);
@@ -103,11 +160,311 @@ describe('ol.format.GeoJSON', function() {
});
describe('#readFeatures', function() {
it('parses feature collection', function() {
var str = JSON.stringify(data),
array = format.readFeatures(str);
expect(array.length).to.be(2);
var first = array[0];
expect(first).to.be.a(ol.Feature);
expect(first.get('LINK_ID')).to.be(573730499);
var firstGeom = first.getGeometry();
expect(firstGeom).to.be.a(ol.geom.LineString);
var second = array[1];
expect(second).to.be.a(ol.Feature);
expect(second.get('ST_NAME')).to.be('BRUNNSGATAN');
var secondGeom = second.getGeometry();
expect(secondGeom).to.be.a(ol.geom.LineString);
});
it('parses countries.geojson', function(done) {
afterLoadText('spec/ol/format/geojson/countries.geojson', function(text) {
var result = format.readFeatures(text);
expect(result.length).to.be(179);
var first = result[0];
expect(first).to.be.a(ol.Feature);
expect(first.get('name')).to.be('Afghanistan');
expect(first.getId()).to.be('AFG');
var firstGeom = first.getGeometry();
expect(firstGeom).to.be.a(ol.geom.Polygon);
expect(ol.extent.equals(firstGeom.getExtent(),
[60.52843, 29.318572, 75.158028, 38.486282]))
.to.be(true);
var last = result[178];
expect(last).to.be.a(ol.Feature);
expect(last.get('name')).to.be('Zimbabwe');
expect(last.getId()).to.be('ZWE');
var lastGeom = last.getGeometry();
expect(lastGeom).to.be.a(ol.geom.Polygon);
expect(ol.extent.equals(lastGeom.getExtent(),
[25.264226, -22.271612, 32.849861, -15.507787]))
.to.be(true);
done();
});
});
it('generates an array of features for Feature', function() {
var format = new ol.format.GeoJSON();
var json = {
type: 'Feature',
properties: {
bam: 'baz'
},
geometry: {
type: 'LineString',
coordinates: [[1, 2], [3, 4]]
}
};
var features = format.readFeatures(json);
expect(features.length).to.be(1);
var first = features[0];
expect(first).to.be.a(ol.Feature);
expect(first.get('bam')).to.be('baz');
expect(first.getGeometry()).to.be.a(ol.geom.LineString);
expect(format.readProjection(json)).to.be(ol.proj.get('EPSG:4326'));
});
});
describe('#readGeometry', function() {
it('parses point', function() {
var str = JSON.stringify({
type: 'Point',
coordinates: [10, 20]
});
var obj = format.readGeometry(str);
expect(obj).to.be.a(ol.geom.Point);
expect(obj.getCoordinates()).to.eql([10, 20]);
});
it('parses linestring', function() {
var str = JSON.stringify({
type: 'LineString',
coordinates: [[10, 20], [30, 40]]
});
var obj = format.readGeometry(str);
expect(obj).to.be.a(ol.geom.LineString);
expect(obj.getCoordinates()).to.eql([[10, 20], [30, 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 = format.readGeometry(str);
expect(obj).to.be.a(ol.geom.Polygon);
var rings = obj.getLinearRings();
expect(rings.length).to.be(3);
expect(rings[0]).to.be.a(ol.geom.LinearRing);
expect(rings[1]).to.be.a(ol.geom.LinearRing);
expect(rings[2]).to.be.a(ol.geom.LinearRing);
});
it.skip('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 = format.readGeometry(str);
expect(array.length).to.be(2);
expect(array[0]).to.be.a(ol.geom.Point);
expect(array[1]).to.be.a(ol.geom.LineString);
});
});
describe('#readProjection', function() {
it('reads named crs from top-level object', function() {
var json = {
type: 'FeatureCollection',
crs: {
type: 'name',
properties: {
name: 'EPSG:3857'
}
},
features: [{
type: 'Feature',
properties: {
foo: 'bar'
},
geometry: {
type: 'Point',
coordinates: [1, 2]
}
}, {
type: 'Feature',
properties: {
bam: 'baz'
},
geometry: {
type: 'LineString',
coordinates: [[1, 2], [3, 4]]
}
}]
};
var features = format.readFeatures(json);
expect(features.length).to.be(2);
var first = features[0];
expect(first).to.be.a(ol.Feature);
expect(first.get('foo')).to.be('bar');
expect(first.getGeometry()).to.be.a(ol.geom.Point);
var second = features[1];
expect(second).to.be.a(ol.Feature);
expect(second.get('bam')).to.be('baz');
expect(second.getGeometry()).to.be.a(ol.geom.LineString);
expect(format.readProjection(json)).to.be(ol.proj.get('EPSG:3857'));
});
it('accepts null crs', function() {
var json = {
type: 'FeatureCollection',
crs: null,
features: [{
type: 'Feature',
properties: {
foo: 'bar'
},
geometry: {
type: 'Point',
coordinates: [1, 2]
}
}, {
type: 'Feature',
properties: {
bam: 'baz'
},
geometry: {
type: 'LineString',
coordinates: [[1, 2], [3, 4]]
}
}]
};
var features = format.readFeatures(json);
expect(features.length).to.be(2);
var first = features[0];
expect(first).to.be.a(ol.Feature);
expect(first.get('foo')).to.be('bar');
expect(first.getGeometry()).to.be.a(ol.geom.Point);
var second = features[1];
expect(second).to.be.a(ol.Feature);
expect(second.get('bam')).to.be('baz');
expect(second.getGeometry()).to.be.a(ol.geom.LineString);
expect(format.readProjection(json)).to.be(ol.proj.get('EPSG:4326'));
});
});
describe('#writeFeatures', function() {
it('encodes feature collection', function() {
var str = JSON.stringify(data),
array = format.readFeatures(str);
var geojson = format.writeFeatures(array);
var result = format.readFeatures(geojson);
expect(array.length).to.equal(result.length);
var got, exp, gotProp, expProp;
for (var i = 0, ii = array.length; i < ii; ++i) {
got = array[i];
exp = result[i];
expect(got.getGeometry().getCoordinates()).to.eql(
exp.getGeometry().getCoordinates());
gotProp = got.getProperties();
delete gotProp.geometry;
expProp = exp.getProperties();
delete expProp.geometry;
expect(gotProp).to.eql(expProp);
}
});
});
describe('#writeGeometry', function() {
it('encodes point', function() {
var point = new ol.geom.Point([10, 20]);
var geojson = format.writeGeometry(point);
expect(point.getCoordinates()).to.eql(
format.readGeometry(geojson).getCoordinates());
});
it('encodes linestring', function() {
var linestring = new ol.geom.LineString([[10, 20], [30, 40]]);
var geojson = format.writeGeometry(linestring);
expect(linestring.getCoordinates()).to.eql(
format.readGeometry(geojson).getCoordinates());
});
it('encodes 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]];
var polygon = new ol.geom.Polygon([outer, inner1, inner2]);
var geojson = format.writeGeometry(polygon);
expect(polygon.getCoordinates()).to.eql(
format.readGeometry(geojson).getCoordinates());
});
it.skip('encodes geometry collection', function() {
var collection = new ol.geom.GeometryCollection([
new ol.geom.Point([10, 20]),
new ol.geom.LineString([[30, 40], [50, 60]])
]);
var geojson = format.writeGeometry(collection);
var got = format.readGeometry(geojson);
var components = collection.getComponents();
expect(components.length).to.equal(got.length);
for (var i = 0, ii = components.length; i < ii; ++i) {
expect(components[i].getCoordinates()).to.eql(got[i].getCoordinates());
}
});
});
});
goog.require('ol.Feature');
goog.require('ol.extent');
goog.require('ol.format.GeoJSON');
goog.require('ol.geom.LineString');
goog.require('ol.geom.LinearRing');
goog.require('ol.geom.Point');
goog.require('ol.geom.Polygon');
goog.require('ol.proj');