Merge pull request #11737 from ahocevar/esrijson-wkid
Write the correct SRS code in EsriJSON
This commit is contained in:
@@ -15,9 +15,8 @@ import {fromLonLat} from '../src/ol/proj.js';
|
|||||||
import {tile as tileStrategy} from '../src/ol/loadingstrategy.js';
|
import {tile as tileStrategy} from '../src/ol/loadingstrategy.js';
|
||||||
|
|
||||||
const serviceUrl =
|
const serviceUrl =
|
||||||
'https://services.arcgis.com/rOo16HdIMeOBI4Mb/arcgis/rest/' +
|
'https://sampleserver6.arcgisonline.com/arcgis/rest/services/Wildfire/FeatureServer/';
|
||||||
'services/PDX_Pedestrian_Districts/FeatureServer/';
|
const layer = '2';
|
||||||
const layer = '0';
|
|
||||||
|
|
||||||
const esrijsonFormat = new EsriJSON();
|
const esrijsonFormat = new EsriJSON();
|
||||||
|
|
||||||
@@ -102,8 +101,8 @@ const map = new Map({
|
|||||||
layers: [raster, vector],
|
layers: [raster, vector],
|
||||||
target: document.getElementById('map'),
|
target: document.getElementById('map'),
|
||||||
view: new View({
|
view: new View({
|
||||||
center: fromLonLat([-122.619, 45.512]),
|
center: fromLonLat([-110.875, 37.345]),
|
||||||
zoom: 12,
|
zoom: 5,
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -123,13 +122,13 @@ const dirty = {};
|
|||||||
selected.on('add', function (evt) {
|
selected.on('add', function (evt) {
|
||||||
const feature = evt.element;
|
const feature = evt.element;
|
||||||
feature.on('change', function (evt) {
|
feature.on('change', function (evt) {
|
||||||
dirty[evt.target.getId()] = true;
|
dirty[evt.target.get('objectid')] = true;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
selected.on('remove', function (evt) {
|
selected.on('remove', function (evt) {
|
||||||
const feature = evt.element;
|
const feature = evt.element;
|
||||||
const fid = feature.getId();
|
const fid = feature.get('objectid');
|
||||||
if (dirty[fid] === true) {
|
if (dirty[fid] === true) {
|
||||||
const payload =
|
const payload =
|
||||||
'[' +
|
'[' +
|
||||||
@@ -139,7 +138,7 @@ selected.on('remove', function (evt) {
|
|||||||
']';
|
']';
|
||||||
const url = serviceUrl + layer + '/updateFeatures';
|
const url = serviceUrl + layer + '/updateFeatures';
|
||||||
$.post(url, {f: 'json', features: payload}).done(function (data) {
|
$.post(url, {f: 'json', features: payload}).done(function (data) {
|
||||||
const result = JSON.parse(data);
|
const result = typeof data === 'string' ? JSON.parse(data) : data;
|
||||||
if (result.updateResults && result.updateResults.length > 0) {
|
if (result.updateResults && result.updateResults.length > 0) {
|
||||||
if (result.updateResults[0].success !== true) {
|
if (result.updateResults[0].success !== true) {
|
||||||
const error = result.updateResults[0].error;
|
const error = result.updateResults[0].error;
|
||||||
@@ -162,11 +161,10 @@ draw.on('drawend', function (evt) {
|
|||||||
']';
|
']';
|
||||||
const url = serviceUrl + layer + '/addFeatures';
|
const url = serviceUrl + layer + '/addFeatures';
|
||||||
$.post(url, {f: 'json', features: payload}).done(function (data) {
|
$.post(url, {f: 'json', features: payload}).done(function (data) {
|
||||||
const result = JSON.parse(data);
|
const result = typeof data === 'string' ? JSON.parse(data) : data;
|
||||||
if (result.addResults && result.addResults.length > 0) {
|
if (result.addResults && result.addResults.length > 0) {
|
||||||
if (result.addResults[0].success === true) {
|
if (result.addResults[0].success === true) {
|
||||||
feature.setId(result.addResults[0]['objectId']);
|
feature.set('objectid', result.addResults[0]['objectId']);
|
||||||
vectorSource.clear();
|
|
||||||
} else {
|
} else {
|
||||||
const error = result.addResults[0].error;
|
const error = result.addResults[0].error;
|
||||||
alert(error.description + ' (' + error.code + ')');
|
alert(error.description + ' (' + error.code + ')');
|
||||||
|
|||||||
@@ -207,16 +207,14 @@ class EsriJSON extends JSONFeature {
|
|||||||
const geometry = feature.getGeometry();
|
const geometry = feature.getGeometry();
|
||||||
if (geometry) {
|
if (geometry) {
|
||||||
object['geometry'] = writeGeometry(geometry, opt_options);
|
object['geometry'] = writeGeometry(geometry, opt_options);
|
||||||
if (opt_options && opt_options.featureProjection) {
|
const projection =
|
||||||
|
opt_options &&
|
||||||
|
(opt_options.dataProjection || opt_options.featureProjection);
|
||||||
|
if (projection) {
|
||||||
object['geometry'][
|
object['geometry'][
|
||||||
'spatialReference'
|
'spatialReference'
|
||||||
] = /** @type {EsriJSONSpatialReferenceWkid} */ ({
|
] = /** @type {EsriJSONSpatialReferenceWkid} */ ({
|
||||||
wkid: Number(
|
wkid: Number(getProjection(projection).getCode().split(':').pop()),
|
||||||
getProjection(opt_options.featureProjection)
|
|
||||||
.getCode()
|
|
||||||
.split(':')
|
|
||||||
.pop()
|
|
||||||
),
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
delete properties[feature.getGeometryName()];
|
delete properties[feature.getGeometryName()];
|
||||||
|
|||||||
@@ -1763,15 +1763,50 @@ describe('ol.format.EsriJSON', function () {
|
|||||||
expect(esrijson.attributes).to.eql({});
|
expect(esrijson.attributes).to.eql({});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('adds the projection inside the geometry correctly', function () {
|
it('adds the projection inside the geometry correctly when featureProjection is set', function () {
|
||||||
const str = JSON.stringify(data);
|
const str = JSON.stringify(data);
|
||||||
const array = format.readFeatures(str);
|
const array = format.readFeatures(str);
|
||||||
const esrijson = format.writeFeaturesObject(array, {
|
const esrijson = format.writeFeaturesObject(array, {
|
||||||
featureProjection: 'EPSG:4326',
|
featureProjection: 'EPSG:3857',
|
||||||
});
|
});
|
||||||
esrijson.features.forEach(function (feature) {
|
esrijson.features.forEach(function (feature, i) {
|
||||||
|
const spatialReference = feature.geometry.spatialReference;
|
||||||
|
expect(Number(spatialReference.wkid)).to.equal(3857);
|
||||||
|
expect(feature.geometry.paths[0]).to.eql(
|
||||||
|
array[i].getGeometry().getCoordinates()
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('adds the projection inside the geometry correctly when dataProjection is set', function () {
|
||||||
|
const str = JSON.stringify(data);
|
||||||
|
const array = format.readFeatures(str);
|
||||||
|
const esrijson = format.writeFeaturesObject(array, {
|
||||||
|
dataProjection: 'EPSG:4326',
|
||||||
|
featureProjection: 'EPSG:3857',
|
||||||
|
});
|
||||||
|
esrijson.features.forEach(function (feature, i) {
|
||||||
const spatialReference = feature.geometry.spatialReference;
|
const spatialReference = feature.geometry.spatialReference;
|
||||||
expect(Number(spatialReference.wkid)).to.equal(4326);
|
expect(Number(spatialReference.wkid)).to.equal(4326);
|
||||||
|
expect(feature.geometry.paths[0]).to.eql(
|
||||||
|
array[i]
|
||||||
|
.getGeometry()
|
||||||
|
.clone()
|
||||||
|
.transform('EPSG:3857', 'EPSG:4326')
|
||||||
|
.getCoordinates()
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('does not add the projection inside the geometry when neither featurProjection nor dataProjection are set', function () {
|
||||||
|
const str = JSON.stringify(data);
|
||||||
|
const array = format.readFeatures(str);
|
||||||
|
const esrijson = format.writeFeaturesObject(array);
|
||||||
|
esrijson.features.forEach(function (feature, i) {
|
||||||
|
expect(feature.geometry.spatialReference).to.be(undefined);
|
||||||
|
expect(feature.geometry.paths[0]).to.eql(
|
||||||
|
array[i].getGeometry().getCoordinates()
|
||||||
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user