Esri JSON read support
This commit is contained in:
1
build.py
1
build.py
@@ -309,6 +309,7 @@ def examples_star_json(name, match):
|
|||||||
"externs/bingmaps.js",
|
"externs/bingmaps.js",
|
||||||
"externs/bootstrap.js",
|
"externs/bootstrap.js",
|
||||||
"externs/closure-compiler.js",
|
"externs/closure-compiler.js",
|
||||||
|
"externs/esrijson.js",
|
||||||
"externs/example.js",
|
"externs/example.js",
|
||||||
"externs/fastclick.js",
|
"externs/fastclick.js",
|
||||||
"externs/geojson.js",
|
"externs/geojson.js",
|
||||||
|
|||||||
@@ -14,6 +14,7 @@
|
|||||||
"externs/bingmaps.js",
|
"externs/bingmaps.js",
|
||||||
"externs/bootstrap.js",
|
"externs/bootstrap.js",
|
||||||
"externs/closure-compiler.js",
|
"externs/closure-compiler.js",
|
||||||
|
"externs/esrijson.js",
|
||||||
"externs/example.js",
|
"externs/example.js",
|
||||||
"externs/fastclick.js",
|
"externs/fastclick.js",
|
||||||
"externs/geojson.js",
|
"externs/geojson.js",
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
"externs": [
|
"externs": [
|
||||||
"externs/bingmaps.js",
|
"externs/bingmaps.js",
|
||||||
"externs/closure-compiler.js",
|
"externs/closure-compiler.js",
|
||||||
|
"externs/esrijson.js",
|
||||||
"externs/geojson.js",
|
"externs/geojson.js",
|
||||||
"externs/oli.js",
|
"externs/oli.js",
|
||||||
"externs/olx.js",
|
"externs/olx.js",
|
||||||
|
|||||||
18
examples_src/vector-esri.html
Normal file
18
examples_src/vector-esri.html
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
---
|
||||||
|
template: example.html
|
||||||
|
title: esri ArcGIS REST Feature Service example
|
||||||
|
shortdesc: Example of using an ArcGIS REST Feature Service with a BBOX strategy.
|
||||||
|
docs: >
|
||||||
|
This example loads new features from ArcGIS REST Feature Service when the view extent changes.
|
||||||
|
tags: "vector, esri, ArcGIS, REST, Feature, Service, bbox, loading, server"
|
||||||
|
---
|
||||||
|
<div class="row-fluid">
|
||||||
|
<div class="span12">
|
||||||
|
<div id="map" class="map"></div>
|
||||||
|
<div class="span4 offset4 pull-right">
|
||||||
|
<div id="info" class="alert alert-success">
|
||||||
|
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
154
examples_src/vector-esri.js
Normal file
154
examples_src/vector-esri.js
Normal file
@@ -0,0 +1,154 @@
|
|||||||
|
goog.require('ol.Attribution');
|
||||||
|
goog.require('ol.Map');
|
||||||
|
goog.require('ol.View');
|
||||||
|
goog.require('ol.format.EsriJSON');
|
||||||
|
goog.require('ol.layer.Tile');
|
||||||
|
goog.require('ol.layer.Vector');
|
||||||
|
goog.require('ol.loadingstrategy');
|
||||||
|
goog.require('ol.proj');
|
||||||
|
goog.require('ol.source.Vector');
|
||||||
|
goog.require('ol.source.XYZ');
|
||||||
|
goog.require('ol.style.Fill');
|
||||||
|
goog.require('ol.style.Stroke');
|
||||||
|
goog.require('ol.style.Style');
|
||||||
|
goog.require('ol.tilegrid.XYZ');
|
||||||
|
|
||||||
|
|
||||||
|
var serviceUrl = 'http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/' +
|
||||||
|
'Petroleum/KSFields/FeatureServer/';
|
||||||
|
var layer = '0';
|
||||||
|
|
||||||
|
var esrijsonFormat = new ol.format.EsriJSON();
|
||||||
|
|
||||||
|
var styleCache = {
|
||||||
|
'ABANDONED': [
|
||||||
|
new ol.style.Style({
|
||||||
|
fill: new ol.style.Fill({
|
||||||
|
color: 'rgba(225, 225, 225, 255)'
|
||||||
|
}),
|
||||||
|
stroke: new ol.style.Stroke({
|
||||||
|
color: 'rgba(0, 0, 0, 255)',
|
||||||
|
width: 0.4
|
||||||
|
})
|
||||||
|
})
|
||||||
|
],
|
||||||
|
'GAS': [
|
||||||
|
new ol.style.Style({
|
||||||
|
fill: new ol.style.Fill({
|
||||||
|
color: 'rgba(255, 0, 0, 255)'
|
||||||
|
}),
|
||||||
|
stroke: new ol.style.Stroke({
|
||||||
|
color: 'rgba(110, 110, 110, 255)',
|
||||||
|
width: 0.4
|
||||||
|
})
|
||||||
|
})
|
||||||
|
],
|
||||||
|
'OIL': [
|
||||||
|
new ol.style.Style({
|
||||||
|
fill: new ol.style.Fill({
|
||||||
|
color: 'rgba(56, 168, 0, 255)'
|
||||||
|
}),
|
||||||
|
stroke: new ol.style.Stroke({
|
||||||
|
color: 'rgba(110, 110, 110, 255)',
|
||||||
|
width: 0
|
||||||
|
})
|
||||||
|
})
|
||||||
|
],
|
||||||
|
'OILGAS': [
|
||||||
|
new ol.style.Style({
|
||||||
|
fill: new ol.style.Fill({
|
||||||
|
color: 'rgba(168, 112, 0, 255)'
|
||||||
|
}),
|
||||||
|
stroke: new ol.style.Stroke({
|
||||||
|
color: 'rgba(110, 110, 110, 255)',
|
||||||
|
width: 0.4
|
||||||
|
})
|
||||||
|
})
|
||||||
|
]
|
||||||
|
};
|
||||||
|
|
||||||
|
var vectorSource = new ol.source.Vector({
|
||||||
|
loader: function(extent, resolution, projection) {
|
||||||
|
var url = serviceUrl + layer + '/query/?f=json&' +
|
||||||
|
'returnGeometry=true&spatialRel=esriSpatialRelIntersects&geometry=' +
|
||||||
|
encodeURIComponent('{"xmin":' + extent[0] + ',"ymin":' +
|
||||||
|
extent[1] + ',"xmax":' + extent[2] + ',"ymax":' + extent[3] +
|
||||||
|
',"spatialReference":{"wkid":102100}}') +
|
||||||
|
'&geometryType=esriGeometryEnvelope&inSR=102100&outFields=*' +
|
||||||
|
'&outSR=102100';
|
||||||
|
$.ajax({url: url, dataType: 'jsonp', success: function(response) {
|
||||||
|
// dataProjection will be read from document
|
||||||
|
var features = esrijsonFormat.readFeatures(response, {
|
||||||
|
featureProjection: projection
|
||||||
|
});
|
||||||
|
if (features.length > 0) {
|
||||||
|
vectorSource.addFeatures(features);
|
||||||
|
}
|
||||||
|
}});
|
||||||
|
},
|
||||||
|
strategy: ol.loadingstrategy.tile(new ol.tilegrid.XYZ({
|
||||||
|
tileSize: 512
|
||||||
|
}))
|
||||||
|
});
|
||||||
|
|
||||||
|
var vector = new ol.layer.Vector({
|
||||||
|
source: vectorSource,
|
||||||
|
style: function(feature, resolution) {
|
||||||
|
var classify = feature.get('activeprod');
|
||||||
|
return styleCache[classify];
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
var attribution = new ol.Attribution({
|
||||||
|
html: 'Tiles © <a href="http://services.arcgisonline.com/ArcGIS/' +
|
||||||
|
'rest/services/World_Topo_Map/MapServer">ArcGIS</a>'
|
||||||
|
});
|
||||||
|
|
||||||
|
var raster = new ol.layer.Tile({
|
||||||
|
source: new ol.source.XYZ({
|
||||||
|
attributions: [attribution],
|
||||||
|
url: 'http://server.arcgisonline.com/ArcGIS/rest/services/' +
|
||||||
|
'World_Topo_Map/MapServer/tile/{z}/{y}/{x}'
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
var map = new ol.Map({
|
||||||
|
layers: [raster, vector],
|
||||||
|
target: document.getElementById('map'),
|
||||||
|
view: new ol.View({
|
||||||
|
center: ol.proj.transform([-97.6114, 38.8403], 'EPSG:4326', 'EPSG:3857'),
|
||||||
|
zoom: 7
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
var displayFeatureInfo = function(pixel) {
|
||||||
|
var features = [];
|
||||||
|
map.forEachFeatureAtPixel(pixel, function(feature, layer) {
|
||||||
|
features.push(feature);
|
||||||
|
});
|
||||||
|
if (features.length > 0) {
|
||||||
|
var info = [];
|
||||||
|
var i, ii;
|
||||||
|
for (i = 0, ii = features.length; i < ii; ++i) {
|
||||||
|
info.push(features[i].get('field_name'));
|
||||||
|
}
|
||||||
|
document.getElementById('info').innerHTML = info.join(', ') || '(unknown)';
|
||||||
|
map.getTarget().style.cursor = 'pointer';
|
||||||
|
} else {
|
||||||
|
document.getElementById('info').innerHTML = ' ';
|
||||||
|
map.getTarget().style.cursor = '';
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
map.on('pointermove', function(evt) {
|
||||||
|
if (evt.dragging) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var pixel = map.getEventPixel(evt.originalEvent);
|
||||||
|
displayFeatureInfo(pixel);
|
||||||
|
});
|
||||||
|
|
||||||
|
map.on('click', function(evt) {
|
||||||
|
displayFeatureInfo(evt.pixel);
|
||||||
|
});
|
||||||
191
externs/esrijson.js
Normal file
191
externs/esrijson.js
Normal file
@@ -0,0 +1,191 @@
|
|||||||
|
|
||||||
|
/**
|
||||||
|
* @fileoverview Externs for EsriJSON.
|
||||||
|
* @see http://resources.arcgis.com/en/help/rest/apiref/geometry.html
|
||||||
|
* @externs
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @constructor
|
||||||
|
*/
|
||||||
|
var EsriJSONObject = function() {};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type {!EsriJSONCRS}
|
||||||
|
*/
|
||||||
|
EsriJSONObject.prototype.spatialReference;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @constructor
|
||||||
|
*/
|
||||||
|
var EsriJSONCRS = function() {};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* CRS well know identifier.
|
||||||
|
* @type {!number}
|
||||||
|
*/
|
||||||
|
EsriJSONCRS.prototype.wkid;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @constructor
|
||||||
|
* @extends {EsriJSONObject}
|
||||||
|
*/
|
||||||
|
var EsriJSONPoint = function() {};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* X coordinate of point.
|
||||||
|
* @type {!number}
|
||||||
|
*/
|
||||||
|
EsriJSONPoint.prototype.x;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Y coordinate of point.
|
||||||
|
* @type {!number}
|
||||||
|
*/
|
||||||
|
EsriJSONPoint.prototype.y;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Z coordinate of point.
|
||||||
|
* @type {number|undefined}
|
||||||
|
*/
|
||||||
|
EsriJSONPoint.prototype.z;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @constructor
|
||||||
|
* @extends {EsriJSONObject}
|
||||||
|
*/
|
||||||
|
var EsriJSONMultipoint = function() {};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Does Multipoint have M values?
|
||||||
|
* @type {boolean|undefined}
|
||||||
|
*/
|
||||||
|
EsriJSONMultipoint.prototype.hasM;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Does Multipoint have Z values?
|
||||||
|
* @type {boolean|undefined}
|
||||||
|
*/
|
||||||
|
EsriJSONMultipoint.prototype.hasZ;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type {!Array.<!Array.<number>>}
|
||||||
|
*/
|
||||||
|
EsriJSONMultipoint.prototype.points;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @constructor
|
||||||
|
* @extends {EsriJSONObject}
|
||||||
|
*/
|
||||||
|
var EsriJSONPolyline = function() {};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Does Polyline have M values?
|
||||||
|
* @type {boolean|undefined}
|
||||||
|
*/
|
||||||
|
EsriJSONPolyline.prototype.hasM;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Does Polyline have Z values?
|
||||||
|
* @type {boolean|undefined}
|
||||||
|
*/
|
||||||
|
EsriJSONPolyline.prototype.hasZ;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type {!Array.<!Array.<!Array.<number>>>}
|
||||||
|
*/
|
||||||
|
EsriJSONPolyline.prototype.paths;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @constructor
|
||||||
|
* @extends {EsriJSONObject}
|
||||||
|
*/
|
||||||
|
var EsriJSONPolygon = function() {};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Does Polygon have M values?
|
||||||
|
* @type {boolean|undefined}
|
||||||
|
*/
|
||||||
|
EsriJSONPolygon.prototype.hasM;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Does Polygon have Z values?
|
||||||
|
* @type {boolean|undefined}
|
||||||
|
*/
|
||||||
|
EsriJSONPolygon.prototype.hasZ;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type {!Array.<!Array.<!Array.<number>>>}
|
||||||
|
*/
|
||||||
|
EsriJSONPolygon.prototype.rings;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @typedef {(EsriJSONPoint|EsriJSONMultipoint|EsriJSONPolyline|
|
||||||
|
EsriJSONPolygon)}
|
||||||
|
*/
|
||||||
|
var EsriJSONGeometry;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @constructor
|
||||||
|
* @extends {EsriJSONObject}
|
||||||
|
*/
|
||||||
|
var EsriJSONFeature = function() {};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type {EsriJSONGeometry}
|
||||||
|
*/
|
||||||
|
EsriJSONFeature.prototype.geometry;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type {Object.<string, *>}
|
||||||
|
*/
|
||||||
|
EsriJSONFeature.prototype.attributes;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @constructor
|
||||||
|
* @extends {EsriJSONObject}
|
||||||
|
*/
|
||||||
|
var EsriJSONFeatureCollection = function() {};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type {!Array.<EsriJSONFeature>}
|
||||||
|
*/
|
||||||
|
EsriJSONFeatureCollection.prototype.features;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The name of the attribute that contains ids.
|
||||||
|
* @type {string}
|
||||||
|
*/
|
||||||
|
EsriJSONFeatureCollection.prototype.objectIdFieldName;
|
||||||
@@ -1635,6 +1635,21 @@ olx.format.GeoJSONOptions.prototype.defaultDataProjection;
|
|||||||
olx.format.GeoJSONOptions.prototype.geometryName;
|
olx.format.GeoJSONOptions.prototype.geometryName;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @typedef {{geometryName: (string|undefined)}}
|
||||||
|
* @api
|
||||||
|
*/
|
||||||
|
olx.format.EsriJSONOptions;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Geometry name to use when creating features.
|
||||||
|
* @type {string|undefined}
|
||||||
|
* @api
|
||||||
|
*/
|
||||||
|
olx.format.EsriJSONOptions.prototype.geometryName;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @typedef {{factor: (number|undefined),
|
* @typedef {{factor: (number|undefined),
|
||||||
* geometryLayout: (ol.geom.GeometryLayout|undefined)}}
|
* geometryLayout: (ol.geom.GeometryLayout|undefined)}}
|
||||||
|
|||||||
368
src/ol/format/esrijsonformat.js
Normal file
368
src/ol/format/esrijsonformat.js
Normal file
@@ -0,0 +1,368 @@
|
|||||||
|
// FIXME add support for hasM
|
||||||
|
goog.provide('ol.format.EsriJSON');
|
||||||
|
|
||||||
|
goog.require('goog.array');
|
||||||
|
goog.require('goog.asserts');
|
||||||
|
goog.require('goog.object');
|
||||||
|
goog.require('ol.Feature');
|
||||||
|
goog.require('ol.extent');
|
||||||
|
goog.require('ol.format.Feature');
|
||||||
|
goog.require('ol.format.JSONFeature');
|
||||||
|
goog.require('ol.geom.GeometryLayout');
|
||||||
|
goog.require('ol.geom.LineString');
|
||||||
|
goog.require('ol.geom.LinearRing');
|
||||||
|
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.geom.flat.orient');
|
||||||
|
goog.require('ol.proj');
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @classdesc
|
||||||
|
* Feature format for reading and writing data in the EsriJSON format.
|
||||||
|
*
|
||||||
|
* @constructor
|
||||||
|
* @extends {ol.format.JSONFeature}
|
||||||
|
* @param {olx.format.EsriJSONOptions=} opt_options Options.
|
||||||
|
* @api
|
||||||
|
*/
|
||||||
|
ol.format.EsriJSON = function(opt_options) {
|
||||||
|
|
||||||
|
var options = goog.isDef(opt_options) ? opt_options : {};
|
||||||
|
|
||||||
|
goog.base(this);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Name of the geometry attribute for features.
|
||||||
|
* @type {string|undefined}
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
this.geometryName_ = options.geometryName;
|
||||||
|
|
||||||
|
};
|
||||||
|
goog.inherits(ol.format.EsriJSON, ol.format.JSONFeature);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {EsriJSONGeometry} object Object.
|
||||||
|
* @param {olx.format.ReadOptions=} opt_options Read options.
|
||||||
|
* @private
|
||||||
|
* @return {ol.geom.Geometry} Geometry.
|
||||||
|
*/
|
||||||
|
ol.format.EsriJSON.readGeometry_ = function(object, opt_options) {
|
||||||
|
if (goog.isNull(object)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
var type;
|
||||||
|
if (goog.isNumber(object.x) && goog.isNumber(object.y)) {
|
||||||
|
type = 'Point';
|
||||||
|
} else if (goog.isDefAndNotNull(object.points)) {
|
||||||
|
type = 'MultiPoint';
|
||||||
|
} else if (goog.isDefAndNotNull(object.paths)) {
|
||||||
|
if (object.paths.length === 1) {
|
||||||
|
type = 'LineString';
|
||||||
|
} else {
|
||||||
|
type = 'MultiLineString';
|
||||||
|
}
|
||||||
|
} else if (goog.isDefAndNotNull(object.rings)) {
|
||||||
|
var rings = ol.format.EsriJSON.convertRings_(object.rings, object.hasZ);
|
||||||
|
object = /** @type {EsriJSONGeometry} */(goog.object.clone(object));
|
||||||
|
if (rings.length === 1) {
|
||||||
|
type = 'Polygon';
|
||||||
|
object.rings = rings[0];
|
||||||
|
} else {
|
||||||
|
type = 'MultiPolygon';
|
||||||
|
object.rings = rings;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
goog.asserts.assert(goog.isDef(type), 'geometry type should be defined');
|
||||||
|
var geometryReader = ol.format.EsriJSON.GEOMETRY_READERS_[type];
|
||||||
|
goog.asserts.assert(goog.isDef(geometryReader),
|
||||||
|
'geometryReader should be defined');
|
||||||
|
return /** @type {ol.geom.Geometry} */ (
|
||||||
|
ol.format.Feature.transformWithOptions(
|
||||||
|
geometryReader(object), false, opt_options));
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determines inner and outer rings.
|
||||||
|
* @param {Array.<!Array.<!Array.<number>>>} rings Rings.
|
||||||
|
* @param {boolean|undefined} hasZ Do rings have Z values in them.
|
||||||
|
* @private
|
||||||
|
* @return {Array.<!Array.<!Array.<number>>>} Transoformed rings.
|
||||||
|
*/
|
||||||
|
ol.format.EsriJSON.convertRings_ = function(rings, hasZ) {
|
||||||
|
var outerRings = [];
|
||||||
|
var holes = [];
|
||||||
|
var i, ii;
|
||||||
|
for (i = 0, ii = rings.length; i < ii; ++i) {
|
||||||
|
var flatRing = goog.array.flatten(rings[i]);
|
||||||
|
var clockwise = ol.geom.flat.orient.linearRingIsClockwise(flatRing, 0,
|
||||||
|
flatRing.length, hasZ === true ? 3 : 2);
|
||||||
|
if (clockwise) {
|
||||||
|
outerRings.push([rings[i]]);
|
||||||
|
} else {
|
||||||
|
holes.push(rings[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
while (holes.length) {
|
||||||
|
var hole = holes.pop();
|
||||||
|
var matched = false;
|
||||||
|
for (i = outerRings.length - 1; i >= 0; i--) {
|
||||||
|
var outerRing = outerRings[i][0];
|
||||||
|
if (ol.extent.containsExtent(new ol.geom.LinearRing(
|
||||||
|
outerRing).getExtent(),
|
||||||
|
new ol.geom.LinearRing(hole).getExtent())) {
|
||||||
|
outerRings[i].push(hole);
|
||||||
|
matched = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!matched) {
|
||||||
|
outerRings.push([hole.reverse()]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return outerRings;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {EsriJSONPoint} object Object.
|
||||||
|
* @private
|
||||||
|
* @return {ol.geom.Point} Point.
|
||||||
|
*/
|
||||||
|
ol.format.EsriJSON.readPointGeometry_ = function(object) {
|
||||||
|
goog.asserts.assert(goog.isNumber(object.x), 'object.x should be number');
|
||||||
|
goog.asserts.assert(goog.isNumber(object.y), 'object.y should be number');
|
||||||
|
var point;
|
||||||
|
if (goog.isDefAndNotNull(object.z)) {
|
||||||
|
point = new ol.geom.Point([object.x, object.y, object.z],
|
||||||
|
ol.geom.GeometryLayout.XYZ);
|
||||||
|
} else {
|
||||||
|
point = new ol.geom.Point([object.x, object.y]);
|
||||||
|
}
|
||||||
|
return point;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {EsriJSONPolyline} object Object.
|
||||||
|
* @private
|
||||||
|
* @return {ol.geom.LineString} LineString.
|
||||||
|
*/
|
||||||
|
ol.format.EsriJSON.readLineStringGeometry_ = function(object) {
|
||||||
|
goog.asserts.assert(goog.isArray(object.paths),
|
||||||
|
'object.paths should be an array');
|
||||||
|
goog.asserts.assert(object.paths.length === 1,
|
||||||
|
'object.paths array length should be 1');
|
||||||
|
return new ol.geom.LineString(object.paths[0].slice(0),
|
||||||
|
object.hasZ === true ? ol.geom.GeometryLayout.XYZ :
|
||||||
|
ol.geom.GeometryLayout.XY);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {EsriJSONPolyline} object Object.
|
||||||
|
* @private
|
||||||
|
* @return {ol.geom.MultiLineString} MultiLineString.
|
||||||
|
*/
|
||||||
|
ol.format.EsriJSON.readMultiLineStringGeometry_ = function(object) {
|
||||||
|
goog.asserts.assert(goog.isArray(object.paths),
|
||||||
|
'object.paths should be an array');
|
||||||
|
goog.asserts.assert(object.paths.length > 1,
|
||||||
|
'object.paths array length should be more than 1');
|
||||||
|
return new ol.geom.MultiLineString(object.paths.slice(0));
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {EsriJSONMultipoint} object Object.
|
||||||
|
* @private
|
||||||
|
* @return {ol.geom.MultiPoint} MultiPoint.
|
||||||
|
*/
|
||||||
|
ol.format.EsriJSON.readMultiPointGeometry_ = function(object) {
|
||||||
|
goog.asserts.assert(goog.isDefAndNotNull(object.points),
|
||||||
|
'object.points should be defined');
|
||||||
|
return new ol.geom.MultiPoint(object.points);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {EsriJSONPolygon} object Object.
|
||||||
|
* @private
|
||||||
|
* @return {ol.geom.MultiPolygon} MultiPolygon.
|
||||||
|
*/
|
||||||
|
ol.format.EsriJSON.readMultiPolygonGeometry_ = function(object) {
|
||||||
|
goog.asserts.assert(goog.isDefAndNotNull(object.rings));
|
||||||
|
goog.asserts.assert(object.rings.length > 1,
|
||||||
|
'object.rings should have length larger than 1');
|
||||||
|
return new ol.geom.MultiPolygon(
|
||||||
|
/** @type {Array.<Array.<Array.<Array.<number>>>>} */(object.rings),
|
||||||
|
object.hasZ === true ? ol.geom.GeometryLayout.XYZ :
|
||||||
|
ol.geom.GeometryLayout.XY);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {EsriJSONPolygon} object Object.
|
||||||
|
* @private
|
||||||
|
* @return {ol.geom.Polygon} Polygon.
|
||||||
|
*/
|
||||||
|
ol.format.EsriJSON.readPolygonGeometry_ = function(object) {
|
||||||
|
goog.asserts.assert(goog.isDefAndNotNull(object.rings));
|
||||||
|
return new ol.geom.Polygon(object.rings,
|
||||||
|
object.hasZ === true ? ol.geom.GeometryLayout.XYZ :
|
||||||
|
ol.geom.GeometryLayout.XY);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @const
|
||||||
|
* @private
|
||||||
|
* @type {Object.<string, function(EsriJSONObject): ol.geom.Geometry>}
|
||||||
|
*/
|
||||||
|
ol.format.EsriJSON.GEOMETRY_READERS_ = {
|
||||||
|
'Point': ol.format.EsriJSON.readPointGeometry_,
|
||||||
|
'LineString': ol.format.EsriJSON.readLineStringGeometry_,
|
||||||
|
'Polygon': ol.format.EsriJSON.readPolygonGeometry_,
|
||||||
|
'MultiPoint': ol.format.EsriJSON.readMultiPointGeometry_,
|
||||||
|
'MultiLineString': ol.format.EsriJSON.readMultiLineStringGeometry_,
|
||||||
|
'MultiPolygon': ol.format.EsriJSON.readMultiPolygonGeometry_
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Read a feature from a EsriJSON Feature source. Only works for Feature,
|
||||||
|
* use `readFeatures` to read FeatureCollection source.
|
||||||
|
*
|
||||||
|
* @function
|
||||||
|
* @param {ArrayBuffer|Document|Node|Object|string} source Source.
|
||||||
|
* @param {olx.format.ReadOptions=} opt_options Read options.
|
||||||
|
* @return {ol.Feature} Feature.
|
||||||
|
* @api stable
|
||||||
|
*/
|
||||||
|
ol.format.EsriJSON.prototype.readFeature;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Read all features from a EsriJSON source. Works with both Feature and
|
||||||
|
* FeatureCollection sources.
|
||||||
|
*
|
||||||
|
* @function
|
||||||
|
* @param {ArrayBuffer|Document|Node|Object|string} source Source.
|
||||||
|
* @param {olx.format.ReadOptions=} opt_options Read options.
|
||||||
|
* @return {Array.<ol.Feature>} Features.
|
||||||
|
* @api stable
|
||||||
|
*/
|
||||||
|
ol.format.EsriJSON.prototype.readFeatures;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritDoc
|
||||||
|
*/
|
||||||
|
ol.format.EsriJSON.prototype.readFeatureFromObject = function(
|
||||||
|
object, opt_options) {
|
||||||
|
var esriJSONFeature = /** @type {EsriJSONFeature} */ (object);
|
||||||
|
goog.asserts.assert(goog.isDefAndNotNull(esriJSONFeature.geometry) ||
|
||||||
|
goog.isDefAndNotNull(esriJSONFeature.compressedGeometry) ||
|
||||||
|
goog.isDefAndNotNull(esriJSONFeature.attributes),
|
||||||
|
'geometry, compressedGeometry or attributes should be defined');
|
||||||
|
var geometry = ol.format.EsriJSON.readGeometry_(esriJSONFeature.geometry,
|
||||||
|
opt_options);
|
||||||
|
var feature = new ol.Feature();
|
||||||
|
if (goog.isDef(this.geometryName_)) {
|
||||||
|
feature.setGeometryName(this.geometryName_);
|
||||||
|
}
|
||||||
|
feature.setGeometry(geometry);
|
||||||
|
if (goog.isDef(opt_options) && goog.isDef(opt_options.idField) &&
|
||||||
|
goog.isDef(esriJSONFeature.attributes[opt_options.idField])) {
|
||||||
|
goog.asserts.assert(
|
||||||
|
goog.isNumber(esriJSONFeature.attributes[opt_options.idField]),
|
||||||
|
'objectIdFieldName value should be a number');
|
||||||
|
feature.setId(/** @type {number} */(
|
||||||
|
esriJSONFeature.attributes[opt_options.idField]));
|
||||||
|
}
|
||||||
|
if (goog.isDef(esriJSONFeature.attributes)) {
|
||||||
|
feature.setProperties(esriJSONFeature.attributes);
|
||||||
|
}
|
||||||
|
return feature;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritDoc
|
||||||
|
*/
|
||||||
|
ol.format.EsriJSON.prototype.readFeaturesFromObject = function(
|
||||||
|
object, opt_options) {
|
||||||
|
var esriJSONObject = /** @type {EsriJSONObject} */ (object);
|
||||||
|
var options = goog.isDef(opt_options) ? opt_options : {};
|
||||||
|
if (goog.isDefAndNotNull(esriJSONObject.features)) {
|
||||||
|
var esriJSONFeatureCollection = /** @type {EsriJSONFeatureCollection} */
|
||||||
|
(object);
|
||||||
|
/** @type {Array.<ol.Feature>} */
|
||||||
|
var features = [];
|
||||||
|
var esriJSONFeatures = esriJSONFeatureCollection.features;
|
||||||
|
var i, ii;
|
||||||
|
options.idField = object.objectIdFieldName;
|
||||||
|
for (i = 0, ii = esriJSONFeatures.length; i < ii; ++i) {
|
||||||
|
features.push(this.readFeatureFromObject(esriJSONFeatures[i],
|
||||||
|
options));
|
||||||
|
}
|
||||||
|
return features;
|
||||||
|
} else {
|
||||||
|
return [this.readFeatureFromObject(object, options)];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Read a geometry from a EsriJSON source.
|
||||||
|
*
|
||||||
|
* @function
|
||||||
|
* @param {ArrayBuffer|Document|Node|Object|string} source Source.
|
||||||
|
* @param {olx.format.ReadOptions=} opt_options Read options.
|
||||||
|
* @return {ol.geom.Geometry} Geometry.
|
||||||
|
* @api stable
|
||||||
|
*/
|
||||||
|
ol.format.EsriJSON.prototype.readGeometry;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritDoc
|
||||||
|
*/
|
||||||
|
ol.format.EsriJSON.prototype.readGeometryFromObject = function(
|
||||||
|
object, opt_options) {
|
||||||
|
return ol.format.EsriJSON.readGeometry_(
|
||||||
|
/** @type {EsriJSONGeometry} */ (object), opt_options);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Read the projection from a EsriJSON source.
|
||||||
|
*
|
||||||
|
* @function
|
||||||
|
* @param {ArrayBuffer|Document|Node|Object|string} source Source.
|
||||||
|
* @return {ol.proj.Projection} Projection.
|
||||||
|
* @api stable
|
||||||
|
*/
|
||||||
|
ol.format.EsriJSON.prototype.readProjection;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritDoc
|
||||||
|
*/
|
||||||
|
ol.format.EsriJSON.prototype.readProjectionFromObject = function(object) {
|
||||||
|
var esriJSONObject = /** @type {EsriJSONObject} */ (object);
|
||||||
|
if (goog.isDefAndNotNull(esriJSONObject.spatialReference) &&
|
||||||
|
goog.isDefAndNotNull(esriJSONObject.spatialReference.wkid)) {
|
||||||
|
var crs = esriJSONObject.spatialReference.wkid;
|
||||||
|
return ol.proj.get('EPSG:' + crs);
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
};
|
||||||
1
test/spec/ol/format/esrijson/ksfields.json
Normal file
1
test/spec/ol/format/esrijson/ksfields.json
Normal file
File diff suppressed because one or more lines are too long
512
test/spec/ol/format/esrijsonformat.test.js
Normal file
512
test/spec/ol/format/esrijsonformat.test.js
Normal file
@@ -0,0 +1,512 @@
|
|||||||
|
goog.provide('ol.test.reader.EsriJSON');
|
||||||
|
|
||||||
|
|
||||||
|
describe('ol.format.EsriJSON', function() {
|
||||||
|
|
||||||
|
var format;
|
||||||
|
beforeEach(function() {
|
||||||
|
format = new ol.format.EsriJSON();
|
||||||
|
});
|
||||||
|
|
||||||
|
var pointEsriJSON = {
|
||||||
|
'geometry': {
|
||||||
|
'x': 102.0,
|
||||||
|
'y': 0.5
|
||||||
|
},
|
||||||
|
'attributes': {
|
||||||
|
'prop0': 'value0'
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
var multiPointEsriJSON = {
|
||||||
|
'geometry': {
|
||||||
|
'points' : [[102.0, 0.0], [103.0, 1.0]]
|
||||||
|
},
|
||||||
|
'attributes': {
|
||||||
|
'prop0': 'value0'
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
var lineStringEsriJSON = {
|
||||||
|
'geometry': {
|
||||||
|
'paths': [[
|
||||||
|
[102.0, 0.0], [103.0, 1.0], [104.0, 0.0], [105.0, 1.0]
|
||||||
|
]]
|
||||||
|
},
|
||||||
|
'attributes': {
|
||||||
|
'prop0': 'value0',
|
||||||
|
'prop1': 0.0
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
var multiLineStringEsriJSON = {
|
||||||
|
'geometry': {
|
||||||
|
'paths': [[
|
||||||
|
[102.0, 0.0], [103.0, 1.0], [104.0, 0.0], [105.0, 1.0]
|
||||||
|
], [
|
||||||
|
[105.0, 3.0], [106.0, 4.0], [107.0, 3.0], [108.0, 4.0]
|
||||||
|
]]
|
||||||
|
},
|
||||||
|
'attributes': {
|
||||||
|
'prop0': 'value0',
|
||||||
|
'prop1': 0.0
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
var polygonEsriJSON = {
|
||||||
|
'geometry': {
|
||||||
|
'rings': [[
|
||||||
|
[100.0, 0.0], [100.0, 1.0], [101.0, 1.0], [101.0, 0.0]
|
||||||
|
]]
|
||||||
|
},
|
||||||
|
'attributes': {
|
||||||
|
'prop0': 'value0',
|
||||||
|
'prop1': {'this': 'that'}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
var multiPolygonEsriJSON = {
|
||||||
|
'geometry': {
|
||||||
|
'rings': [
|
||||||
|
[
|
||||||
|
[0, 1],
|
||||||
|
[1, 4],
|
||||||
|
[4, 3],
|
||||||
|
[3, 0]
|
||||||
|
], [
|
||||||
|
[2, 2],
|
||||||
|
[3, 2],
|
||||||
|
[3, 3],
|
||||||
|
[2, 3]
|
||||||
|
], [
|
||||||
|
[10, 1],
|
||||||
|
[11, 5],
|
||||||
|
[14, 3],
|
||||||
|
[13, 0]
|
||||||
|
]
|
||||||
|
]
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
var featureCollectionEsriJSON = {
|
||||||
|
'features': [pointEsriJSON, lineStringEsriJSON, polygonEsriJSON]
|
||||||
|
};
|
||||||
|
|
||||||
|
var data = {
|
||||||
|
'features': [
|
||||||
|
{
|
||||||
|
'attributes': {
|
||||||
|
'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': {
|
||||||
|
'paths': [[
|
||||||
|
[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]
|
||||||
|
]]
|
||||||
|
}
|
||||||
|
}, {
|
||||||
|
'attributes': {
|
||||||
|
'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': {
|
||||||
|
'paths': [[
|
||||||
|
[1549754.2769, 6403854.8024],
|
||||||
|
[1549728.45985, 6403920.2]
|
||||||
|
]]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
};
|
||||||
|
|
||||||
|
describe('#readFeature', function() {
|
||||||
|
|
||||||
|
it('can read a single point feature', function() {
|
||||||
|
var feature = format.readFeature(pointEsriJSON);
|
||||||
|
expect(feature).to.be.an(ol.Feature);
|
||||||
|
var geometry = feature.getGeometry();
|
||||||
|
expect(geometry).to.be.an(ol.geom.Point);
|
||||||
|
expect(geometry.getCoordinates()).to.eql([102.0, 0.5]);
|
||||||
|
expect(feature.get('prop0')).to.be('value0');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('can read a single multipoint feature', function() {
|
||||||
|
var feature = format.readFeature(multiPointEsriJSON);
|
||||||
|
expect(feature).to.be.an(ol.Feature);
|
||||||
|
var geometry = feature.getGeometry();
|
||||||
|
expect(geometry).to.be.an(ol.geom.MultiPoint);
|
||||||
|
expect(geometry.getCoordinates()).to.eql([[102.0, 0.0] , [103.0, 1.0]]);
|
||||||
|
expect(feature.get('prop0')).to.be('value0');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('can read a single line string feature', function() {
|
||||||
|
var feature = format.readFeature(lineStringEsriJSON);
|
||||||
|
expect(feature).to.be.an(ol.Feature);
|
||||||
|
var geometry = feature.getGeometry();
|
||||||
|
expect(geometry).to.be.an(ol.geom.LineString);
|
||||||
|
expect(geometry.getCoordinates()).to.eql(
|
||||||
|
[[102.0, 0.0], [103.0, 1.0], [104.0, 0.0], [105.0, 1.0]]);
|
||||||
|
expect(feature.get('prop0')).to.be('value0');
|
||||||
|
expect(feature.get('prop1')).to.be(0.0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('can read a multi line string feature', function() {
|
||||||
|
var feature = format.readFeature(multiLineStringEsriJSON);
|
||||||
|
expect(feature).to.be.an(ol.Feature);
|
||||||
|
var geometry = feature.getGeometry();
|
||||||
|
expect(geometry).to.be.an(ol.geom.MultiLineString);
|
||||||
|
expect(geometry.getCoordinates()).to.eql([
|
||||||
|
[[102.0, 0.0], [103.0, 1.0], [104.0, 0.0], [105.0, 1.0]],
|
||||||
|
[[105.0, 3.0], [106.0, 4.0], [107.0, 3.0], [108.0, 4.0]]
|
||||||
|
]);
|
||||||
|
expect(feature.get('prop0')).to.be('value0');
|
||||||
|
expect(feature.get('prop1')).to.be(0.0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('can read a single polygon feature', function() {
|
||||||
|
var feature = format.readFeature(polygonEsriJSON);
|
||||||
|
expect(feature).to.be.an(ol.Feature);
|
||||||
|
var geometry = feature.getGeometry();
|
||||||
|
expect(geometry).to.be.an(ol.geom.Polygon);
|
||||||
|
expect(geometry.getCoordinates()).to.eql([[
|
||||||
|
[100.0, 0.0], [100.0, 1.0], [101.0, 1.0], [101.0, 0.0]
|
||||||
|
]]);
|
||||||
|
expect(feature.get('prop0')).to.be('value0');
|
||||||
|
expect(feature.get('prop1')).to.eql({'this': 'that'});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('can read a multi polygon feature', function() {
|
||||||
|
var feature = format.readFeature(multiPolygonEsriJSON);
|
||||||
|
expect(feature).to.be.an(ol.Feature);
|
||||||
|
var geometry = feature.getGeometry();
|
||||||
|
expect(geometry).to.be.an(ol.geom.MultiPolygon);
|
||||||
|
expect(geometry.getCoordinates()).to.eql([
|
||||||
|
[[[0, 1], [1, 4], [4, 3], [3, 0]], [[2, 2], [3, 2], [3, 3], [2, 3]]],
|
||||||
|
[[[10, 1], [11, 5], [14, 3], [13, 0]]]
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('can read a feature collection', function() {
|
||||||
|
var features = format.readFeatures(featureCollectionEsriJSON);
|
||||||
|
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);
|
||||||
|
expect(features[2].getGeometry()).to.be.an(ol.geom.Polygon);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('can read and transform a point', function() {
|
||||||
|
var feature = format.readFeatures(pointEsriJSON, {
|
||||||
|
featureProjection: 'EPSG:3857',
|
||||||
|
dataProjection: 'EPSG:4326'
|
||||||
|
});
|
||||||
|
expect(feature[0].getGeometry()).to.be.an(ol.geom.Point);
|
||||||
|
expect(feature[0].getGeometry().getCoordinates()).to.eql(
|
||||||
|
ol.proj.transform([102.0, 0.5], 'EPSG:4326', 'EPSG:3857'));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('can read and transform a feature collection', function() {
|
||||||
|
var features = format.readFeatures(featureCollectionEsriJSON, {
|
||||||
|
featureProjection: 'EPSG:3857',
|
||||||
|
dataProjection: 'EPSG:4326'
|
||||||
|
});
|
||||||
|
expect(features[0].getGeometry()).to.be.an(ol.geom.Point);
|
||||||
|
expect(features[0].getGeometry().getCoordinates()).to.eql(
|
||||||
|
ol.proj.transform([102.0, 0.5], 'EPSG:4326', 'EPSG:3857'));
|
||||||
|
expect(features[1].getGeometry().getCoordinates()).to.eql([
|
||||||
|
ol.proj.transform([102.0, 0.0], 'EPSG:4326', 'EPSG:3857'),
|
||||||
|
ol.proj.transform([103.0, 1.0], 'EPSG:4326', 'EPSG:3857'),
|
||||||
|
ol.proj.transform([104.0, 0.0], 'EPSG:4326', 'EPSG:3857'),
|
||||||
|
ol.proj.transform([105.0, 1.0], 'EPSG:4326', 'EPSG:3857')
|
||||||
|
]);
|
||||||
|
expect(features[2].getGeometry().getCoordinates()).to.eql([[
|
||||||
|
ol.proj.transform([100.0, 0.0], 'EPSG:4326', 'EPSG:3857'),
|
||||||
|
ol.proj.transform([100.0, 1.0], 'EPSG:4326', 'EPSG:3857'),
|
||||||
|
ol.proj.transform([101.0, 1.0], 'EPSG:4326', 'EPSG:3857'),
|
||||||
|
ol.proj.transform([101.0, 0.0], 'EPSG:4326', 'EPSG:3857')
|
||||||
|
]]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('can create a feature with a specific geometryName', function() {
|
||||||
|
var feature = new ol.format.EsriJSON({geometryName: 'the_geom'}).
|
||||||
|
readFeature(pointEsriJSON);
|
||||||
|
expect(feature.getGeometryName()).to.be('the_geom');
|
||||||
|
expect(feature.getGeometry()).to.be.an(ol.geom.Point);
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
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 ksfields.geojson', function(done) {
|
||||||
|
afterLoadText('spec/ol/format/esrijson/ksfields.json', function(text) {
|
||||||
|
var result = format.readFeatures(text);
|
||||||
|
expect(result.length).to.be(306);
|
||||||
|
|
||||||
|
var first = result[0];
|
||||||
|
expect(first).to.be.a(ol.Feature);
|
||||||
|
expect(first.get('field_name')).to.be('EUDORA');
|
||||||
|
expect(first.getId()).to.be(6406);
|
||||||
|
var firstGeom = first.getGeometry();
|
||||||
|
expect(firstGeom).to.be.a(ol.geom.Polygon);
|
||||||
|
expect(ol.extent.equals(firstGeom.getExtent(),
|
||||||
|
[-10585772.743554419, 4712365.161160459,
|
||||||
|
-10579560.16462974, 4716567.373073828]))
|
||||||
|
.to.be(true);
|
||||||
|
|
||||||
|
var last = result[305];
|
||||||
|
expect(last).to.be.a(ol.Feature);
|
||||||
|
expect(last.get('field_name')).to.be('PAOLA-RANTOUL');
|
||||||
|
expect(last.getId()).to.be(223);
|
||||||
|
var lastGeom = last.getGeometry();
|
||||||
|
expect(lastGeom).to.be.a(ol.geom.Polygon);
|
||||||
|
expect(ol.extent.equals(lastGeom.getExtent(),
|
||||||
|
[-10596945.530910717, 4634530.860533288,
|
||||||
|
-10538217.991305258, 4691558.678837225]))
|
||||||
|
.to.be(true);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('#readGeometry', function() {
|
||||||
|
|
||||||
|
it('parses point', function() {
|
||||||
|
var str = JSON.stringify({
|
||||||
|
x: 10,
|
||||||
|
y: 20
|
||||||
|
});
|
||||||
|
|
||||||
|
var obj = format.readGeometry(str);
|
||||||
|
expect(obj).to.be.a(ol.geom.Point);
|
||||||
|
expect(obj.getCoordinates()).to.eql([10, 20]);
|
||||||
|
expect(obj.getLayout()).to.eql(ol.geom.GeometryLayout.XY);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('parses XYZ point', function() {
|
||||||
|
var str = JSON.stringify({
|
||||||
|
x: 10,
|
||||||
|
y: 20,
|
||||||
|
z: 10
|
||||||
|
});
|
||||||
|
|
||||||
|
var obj = format.readGeometry(str);
|
||||||
|
expect(obj).to.be.a(ol.geom.Point);
|
||||||
|
expect(obj.getCoordinates()).to.eql([10, 20, 10]);
|
||||||
|
expect(obj.getLayout()).to.eql(ol.geom.GeometryLayout.XYZ);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('parses multipoint', function() {
|
||||||
|
var str = JSON.stringify({
|
||||||
|
points: [[10, 20], [20, 30]]
|
||||||
|
});
|
||||||
|
|
||||||
|
var obj = format.readGeometry(str);
|
||||||
|
expect(obj).to.be.a(ol.geom.MultiPoint);
|
||||||
|
expect(obj.getCoordinates()).to.eql([[10, 20], [20, 30]]);
|
||||||
|
expect(obj.getLayout()).to.eql(ol.geom.GeometryLayout.XY);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('parses XYZ multipoint', function() {
|
||||||
|
var str = JSON.stringify({
|
||||||
|
points: [[10, 20, 0], [20, 30, 0]],
|
||||||
|
hasZ: true
|
||||||
|
});
|
||||||
|
|
||||||
|
var obj = format.readGeometry(str);
|
||||||
|
expect(obj).to.be.a(ol.geom.MultiPoint);
|
||||||
|
expect(obj.getCoordinates()).to.eql([[10, 20, 0], [20, 30, 0]]);
|
||||||
|
expect(obj.getLayout()).to.eql(ol.geom.GeometryLayout.XYZ);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('parses linestring', function() {
|
||||||
|
var str = JSON.stringify({
|
||||||
|
paths: [[[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]]);
|
||||||
|
expect(obj.getLayout()).to.eql(ol.geom.GeometryLayout.XY);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('parses XYZ linestring', function() {
|
||||||
|
var str = JSON.stringify({
|
||||||
|
hasZ: true,
|
||||||
|
paths: [[[10, 20, 1534], [30, 40, 1420]]]
|
||||||
|
});
|
||||||
|
|
||||||
|
var obj = format.readGeometry(str);
|
||||||
|
expect(obj).to.be.a(ol.geom.LineString);
|
||||||
|
expect(obj.getLayout()).to.eql(ol.geom.GeometryLayout.XYZ);
|
||||||
|
expect(obj.getCoordinates()).to.eql([[10, 20, 1534], [30, 40, 1420]]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('parses polygon', function() {
|
||||||
|
var outer = [[0, 0], [0, 10], [10, 10], [10, 0], [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({
|
||||||
|
rings: [outer, inner1, inner2]
|
||||||
|
});
|
||||||
|
var obj = format.readGeometry(str);
|
||||||
|
expect(obj).to.be.a(ol.geom.Polygon);
|
||||||
|
expect(obj.getLayout()).to.eql(ol.geom.GeometryLayout.XY);
|
||||||
|
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('parses XYZ polygon', function() {
|
||||||
|
var outer = [[0, 0, 5], [0, 10, 5], [10, 10, 5], [10, 0, 5], [0, 0, 5]],
|
||||||
|
inner1 = [[1, 1, 3], [2, 1, 3], [2, 2, 3], [1, 2, 3], [1, 1, 3]],
|
||||||
|
inner2 = [[8, 8, 2], [9, 8, 2], [9, 9, 2], [8, 9, 2], [8, 8, 2]],
|
||||||
|
str = JSON.stringify({
|
||||||
|
rings: [outer, inner1, inner2],
|
||||||
|
hasZ: true
|
||||||
|
});
|
||||||
|
var obj = format.readGeometry(str);
|
||||||
|
expect(obj).to.be.a(ol.geom.Polygon);
|
||||||
|
expect(obj.getLayout()).to.eql(ol.geom.GeometryLayout.XYZ);
|
||||||
|
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('parses XYZ multipolygon', function() {
|
||||||
|
var str = JSON.stringify({
|
||||||
|
rings: [
|
||||||
|
[[0, 1, 0], [1, 4, 0], [4, 3, 0], [3, 0, 0]],
|
||||||
|
[[2, 2, 0], [3, 2, 0], [3, 3, 0], [2, 3, 0]],
|
||||||
|
[[10, 1, 0], [11, 5, 0], [14, 3, 0], [13, 0, 0]]
|
||||||
|
],
|
||||||
|
hasZ: true
|
||||||
|
});
|
||||||
|
var obj = format.readGeometry(str);
|
||||||
|
expect(obj).to.be.a(ol.geom.MultiPolygon);
|
||||||
|
expect(obj.getLayout()).to.eql(ol.geom.GeometryLayout.XYZ);
|
||||||
|
expect(obj.getCoordinates()).to.eql([
|
||||||
|
[[[0, 1, 0], [1, 4, 0], [4, 3, 0], [3, 0, 0]], [[2, 2, 0], [3, 2, 0],
|
||||||
|
[3, 3, 0], [2, 3, 0]]],
|
||||||
|
[[[10, 1, 0], [11, 5, 0], [14, 3, 0], [13, 0, 0]]]
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('#readProjection', function() {
|
||||||
|
|
||||||
|
it('reads named crs from top-level object', function() {
|
||||||
|
|
||||||
|
var json = {
|
||||||
|
spatialReference: {
|
||||||
|
wkid: 3857
|
||||||
|
},
|
||||||
|
features: [{
|
||||||
|
attributes: {
|
||||||
|
foo: 'bar'
|
||||||
|
},
|
||||||
|
geometry: {
|
||||||
|
x: 1,
|
||||||
|
y: 2
|
||||||
|
}
|
||||||
|
}, {
|
||||||
|
attributes: {
|
||||||
|
bam: 'baz'
|
||||||
|
},
|
||||||
|
geometry: {
|
||||||
|
paths: [[[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'));
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
goog.require('ol.Feature');
|
||||||
|
goog.require('ol.extent');
|
||||||
|
goog.require('ol.format.EsriJSON');
|
||||||
|
goog.require('ol.geom.GeometryLayout');
|
||||||
|
goog.require('ol.geom.LineString');
|
||||||
|
goog.require('ol.geom.LinearRing');
|
||||||
|
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');
|
||||||
Reference in New Issue
Block a user