Add target/sourceProjection to ol.format.GeoJSON#read*
This commit is contained in:
committed by
Andreas Hocevar
parent
d13261a21a
commit
225791c969
@@ -64,28 +64,46 @@ ol.format.GeoJSON.EXTENSIONS_ = ['.geojson'];
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {GeoJSONObject} object Object.
|
* @param {GeoJSONObject} object Object.
|
||||||
|
* @param {ol.proj.Projection} targetProjection Target projection.
|
||||||
|
* @param {ol.proj.Projection} sourceProjection Source projection.
|
||||||
* @private
|
* @private
|
||||||
* @return {ol.geom.Geometry} Geometry.
|
* @return {ol.geom.Geometry} Geometry.
|
||||||
*/
|
*/
|
||||||
ol.format.GeoJSON.readGeometry_ = function(object) {
|
ol.format.GeoJSON.readGeometry_ = function(
|
||||||
|
object, targetProjection, sourceProjection) {
|
||||||
if (goog.isNull(object)) {
|
if (goog.isNull(object)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
var geometryReader = ol.format.GeoJSON.GEOMETRY_READERS_[object.type];
|
var geometryReader = ol.format.GeoJSON.GEOMETRY_READERS_[object.type];
|
||||||
goog.asserts.assert(goog.isDef(geometryReader));
|
goog.asserts.assert(goog.isDef(geometryReader));
|
||||||
return geometryReader(object);
|
var geometry = geometryReader(object);
|
||||||
|
if (!goog.isNull(targetProjection) && !goog.isNull(sourceProjection) &&
|
||||||
|
!ol.proj.equivalent(targetProjection, sourceProjection)) {
|
||||||
|
geometry.transform(sourceProjection, targetProjection);
|
||||||
|
}
|
||||||
|
return geometry;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {GeoJSONGeometryCollection} object Object.
|
* @param {GeoJSONGeometryCollection} object Object.
|
||||||
|
* @param {ol.proj.Projection} targetProjection Target projection.
|
||||||
|
* @param {ol.proj.Projection} sourceProjection Source projection.
|
||||||
* @private
|
* @private
|
||||||
* @return {ol.geom.GeometryCollection} Geometry collection.
|
* @return {ol.geom.GeometryCollection} Geometry collection.
|
||||||
*/
|
*/
|
||||||
ol.format.GeoJSON.readGeometryCollectionGeometry_ = function(object) {
|
ol.format.GeoJSON.readGeometryCollectionGeometry_ = function(
|
||||||
|
object, targetProjection, sourceProjection) {
|
||||||
goog.asserts.assert(object.type == 'GeometryCollection');
|
goog.asserts.assert(object.type == 'GeometryCollection');
|
||||||
var geometries = goog.array.map(
|
var geometries = goog.array.map(object.geometries,
|
||||||
object.geometries, ol.format.GeoJSON.readGeometry_);
|
/**
|
||||||
|
* @param {GeoJSONObject} geometry Geometry.
|
||||||
|
* @return {ol.geom.Geometry} geometry Geometry.
|
||||||
|
*/
|
||||||
|
function(geometry) {
|
||||||
|
return ol.format.GeoJSON.readGeometry_(
|
||||||
|
geometry, targetProjection, sourceProjection);
|
||||||
|
});
|
||||||
return new ol.geom.GeometryCollection(geometries);
|
return new ol.geom.GeometryCollection(geometries);
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -330,6 +348,8 @@ ol.format.GeoJSON.prototype.getExtensions = function() {
|
|||||||
*
|
*
|
||||||
* @function
|
* @function
|
||||||
* @param {ArrayBuffer|Document|Node|Object|string} source Source.
|
* @param {ArrayBuffer|Document|Node|Object|string} source Source.
|
||||||
|
* @param {ol.proj.ProjectionLike=} opt_targetProjection Target projection.
|
||||||
|
* @param {ol.proj.ProjectionLike=} opt_sourceProjection Source projection.
|
||||||
* @return {ol.Feature} Feature.
|
* @return {ol.Feature} Feature.
|
||||||
* @api
|
* @api
|
||||||
*/
|
*/
|
||||||
@@ -342,6 +362,8 @@ ol.format.GeoJSON.prototype.readFeature;
|
|||||||
*
|
*
|
||||||
* @function
|
* @function
|
||||||
* @param {ArrayBuffer|Document|Node|Object|string} source Source.
|
* @param {ArrayBuffer|Document|Node|Object|string} source Source.
|
||||||
|
* @param {ol.proj.ProjectionLike=} opt_targetProjection Target projection.
|
||||||
|
* @param {ol.proj.ProjectionLike=} opt_sourceProjection Source projection.
|
||||||
* @return {Array.<ol.Feature>} Features.
|
* @return {Array.<ol.Feature>} Features.
|
||||||
* @api
|
* @api
|
||||||
*/
|
*/
|
||||||
@@ -351,10 +373,17 @@ ol.format.GeoJSON.prototype.readFeatures;
|
|||||||
/**
|
/**
|
||||||
* @inheritDoc
|
* @inheritDoc
|
||||||
*/
|
*/
|
||||||
ol.format.GeoJSON.prototype.readFeatureFromObject = function(object) {
|
ol.format.GeoJSON.prototype.readFeatureFromObject = function(
|
||||||
|
object, opt_targetProjection, opt_sourceProjection) {
|
||||||
var geoJSONFeature = /** @type {GeoJSONFeature} */ (object);
|
var geoJSONFeature = /** @type {GeoJSONFeature} */ (object);
|
||||||
goog.asserts.assert(geoJSONFeature.type == 'Feature');
|
goog.asserts.assert(geoJSONFeature.type == 'Feature');
|
||||||
var geometry = ol.format.GeoJSON.readGeometry_(geoJSONFeature.geometry);
|
var targetProjection = goog.isDef(opt_targetProjection) ?
|
||||||
|
ol.proj.get(opt_targetProjection) : null;
|
||||||
|
var sourceProjection = goog.isDef(opt_sourceProjection) ?
|
||||||
|
ol.proj.get(opt_sourceProjection) :
|
||||||
|
this.readProjectionFromObject(object);
|
||||||
|
var geometry = ol.format.GeoJSON.readGeometry_(geoJSONFeature.geometry,
|
||||||
|
targetProjection, sourceProjection);
|
||||||
var feature = new ol.Feature();
|
var feature = new ol.Feature();
|
||||||
if (goog.isDef(this.geometryName_)) {
|
if (goog.isDef(this.geometryName_)) {
|
||||||
feature.setGeometryName(this.geometryName_);
|
feature.setGeometryName(this.geometryName_);
|
||||||
@@ -373,7 +402,8 @@ ol.format.GeoJSON.prototype.readFeatureFromObject = function(object) {
|
|||||||
/**
|
/**
|
||||||
* @inheritDoc
|
* @inheritDoc
|
||||||
*/
|
*/
|
||||||
ol.format.GeoJSON.prototype.readFeaturesFromObject = function(object) {
|
ol.format.GeoJSON.prototype.readFeaturesFromObject = function(
|
||||||
|
object, opt_targetProjection, opt_sourceProjection) {
|
||||||
var geoJSONObject = /** @type {GeoJSONObject} */ (object);
|
var geoJSONObject = /** @type {GeoJSONObject} */ (object);
|
||||||
if (geoJSONObject.type == 'Feature') {
|
if (geoJSONObject.type == 'Feature') {
|
||||||
return [this.readFeatureFromObject(object)];
|
return [this.readFeatureFromObject(object)];
|
||||||
@@ -385,7 +415,8 @@ ol.format.GeoJSON.prototype.readFeaturesFromObject = function(object) {
|
|||||||
var geoJSONFeatures = geoJSONFeatureCollection.features;
|
var geoJSONFeatures = geoJSONFeatureCollection.features;
|
||||||
var i, ii;
|
var i, ii;
|
||||||
for (i = 0, ii = geoJSONFeatures.length; i < ii; ++i) {
|
for (i = 0, ii = geoJSONFeatures.length; i < ii; ++i) {
|
||||||
features.push(this.readFeatureFromObject(geoJSONFeatures[i]));
|
features.push(this.readFeatureFromObject(geoJSONFeatures[i],
|
||||||
|
opt_targetProjection, opt_sourceProjection));
|
||||||
}
|
}
|
||||||
return features;
|
return features;
|
||||||
} else {
|
} else {
|
||||||
@@ -400,6 +431,8 @@ ol.format.GeoJSON.prototype.readFeaturesFromObject = function(object) {
|
|||||||
*
|
*
|
||||||
* @function
|
* @function
|
||||||
* @param {ArrayBuffer|Document|Node|Object|string} source Source.
|
* @param {ArrayBuffer|Document|Node|Object|string} source Source.
|
||||||
|
* @param {ol.proj.ProjectionLike=} opt_targetProjection Target projection.
|
||||||
|
* @param {ol.proj.ProjectionLike=} opt_sourceProjection Source projection.
|
||||||
* @return {ol.geom.Geometry} Geometry.
|
* @return {ol.geom.Geometry} Geometry.
|
||||||
* @api
|
* @api
|
||||||
*/
|
*/
|
||||||
@@ -409,9 +442,16 @@ ol.format.GeoJSON.prototype.readGeometry;
|
|||||||
/**
|
/**
|
||||||
* @inheritDoc
|
* @inheritDoc
|
||||||
*/
|
*/
|
||||||
ol.format.GeoJSON.prototype.readGeometryFromObject = function(object) {
|
ol.format.GeoJSON.prototype.readGeometryFromObject = function(
|
||||||
|
object, opt_targetProjection, opt_sourceProjection) {
|
||||||
|
var targetProjection = goog.isDef(opt_targetProjection) ?
|
||||||
|
ol.proj.get(opt_targetProjection) : null;
|
||||||
|
var sourceProjection = goog.isDef(opt_sourceProjection) ?
|
||||||
|
ol.proj.get(opt_sourceProjection) :
|
||||||
|
this.readProjectionFromObject(object);
|
||||||
return ol.format.GeoJSON.readGeometry_(
|
return ol.format.GeoJSON.readGeometry_(
|
||||||
/** @type {GeoJSONGeometry} */ (object));
|
/** @type {GeoJSONGeometry} */ (object),
|
||||||
|
targetProjection, sourceProjection);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user