Rename ol.reader to ol.format
This commit is contained in:
17
src/ol/format/format.js
Normal file
17
src/ol/format/format.js
Normal file
@@ -0,0 +1,17 @@
|
||||
goog.provide('ol.format');
|
||||
|
||||
|
||||
/**
|
||||
* @param {function(Object, function(this: S, ol.Feature): T, S=)} reader
|
||||
* Reader.
|
||||
* @param {Object} object Object.
|
||||
* @return {Array.<ol.Feature>}
|
||||
* @template S,T
|
||||
*/
|
||||
ol.format.readAllFromObject = function(reader, object) {
|
||||
var features = [];
|
||||
reader(object, function(feature) {
|
||||
features.push(feature);
|
||||
});
|
||||
return features;
|
||||
};
|
||||
176
src/ol/format/geojson/geojsonformat.js
Normal file
176
src/ol/format/geojson/geojsonformat.js
Normal file
@@ -0,0 +1,176 @@
|
||||
// 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.geom.LineString');
|
||||
goog.require('ol.geom.MultiLineString');
|
||||
goog.require('ol.geom.MultiPolygon');
|
||||
goog.require('ol.geom.Point');
|
||||
goog.require('ol.geom.Polygon');
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @constructor
|
||||
*/
|
||||
ol.format.GeoJSON = function() {
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @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);
|
||||
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;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {GeoJSONObject} object Object.
|
||||
* @param {function(this: S, ol.Feature): T} callback Callback.
|
||||
* @param {S=} opt_obj Scope.
|
||||
* @return {T} Callback result.
|
||||
* @template S,T
|
||||
*/
|
||||
ol.format.GeoJSON.readObject = function(object, callback, opt_obj) {
|
||||
var objectReader = ol.format.GeoJSON.OBJECT_READERS_[object.type];
|
||||
goog.asserts.assert(goog.isDef(objectReader));
|
||||
return objectReader(object, callback, opt_obj);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {string} string String.
|
||||
* @param {function(ol.Feature): T} callback Callback.
|
||||
* @param {S=} opt_obj Scope.
|
||||
* @return {T} Callback result.
|
||||
* @template S,T
|
||||
*/
|
||||
ol.format.GeoJSON.readString = function(string, callback, opt_obj) {
|
||||
var object = goog.json.parse(string);
|
||||
return ol.format.GeoJSON.readObject(
|
||||
/** @type {GeoJSONObject} */ (object), callback);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @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_
|
||||
};
|
||||
Reference in New Issue
Block a user