我如何访问 GeoJson 中的几何图形
How i can access geometry inside GeoJson
再次学习 deep openlayers-6,现在我想从作为层加载的 GeoJson 文件访问几何图形,我该怎么做?在此先感谢您的帮助。
import 'ol/ol.css';
import Feature from 'ol/Feature';
import Geolocation from 'ol/Geolocation';
import Map from 'ol/Map';
import View from 'ol/View';
import Point from 'ol/geom/Point';
import TileLayer from 'ol/layer/Tile';
import VectorLayer from 'ol/layer/Vector';
import OSM from 'ol/source/OSM';
import VectorSource from 'ol/source/Vector';
import {Circle as CircleStyle, Fill, Stroke, Style} from 'ol/style';
import GeoJSON from 'ol/format/GeoJSON';
var view = new View({
center: [0, 0],
zoom: 2
});
var openstreet = new TileLayer({
source: new OSM()
});
var geozonas = new VectorLayer({
source: new VectorSource({
url:'https://geo.anantara.cl/maps/json/geozone2.json',
format: new GeoJSON()
})
});
var map = new Map({
layers: [
openstreet, geozonas
],
target: 'map',
view: view
});
继续调查,我发现了一个名为 featureloader.js 的文件,其中调用了一个读取 geojson 文件的函数,但我找不到它的存储位置,显然他并没有原封不动地保留它,而是显然进行了一种计算。您还有其他相关信息吗?
export function loadFeaturesXhr(url, format, success, failure) {
return (
/**
* @param {import("./extent.js").Extent} extent Extent.
* @param {number} resolution Resolution.
* @param {import("./proj/Projection.js").default} projection Projection.
* @this {import("./source/Vector").default|import("./VectorTile.js").default}
*/
function (extent, resolution, projection) {
var xhr = new XMLHttpRequest();
console.log(new Error().stack); //psilva
xhr.open('GET', typeof url === 'function' ? url(extent, resolution, projection) : url, true);
if (format.getType() == FormatType.ARRAY_BUFFER) {
xhr.responseType = 'arraybuffer';
}
xhr.withCredentials = withCredentials;
/**
* @param {Event} event Event.
* @private
*/
xhr.onload = function (event) {
// status will be 0 for file:// urls
if (!xhr.status || xhr.status >= 200 && xhr.status < 300) {
var type = format.getType();
/** @type {Document|Node|Object|string|undefined} */
var source = void 0;
if (type == FormatType.JSON || type == FormatType.TEXT) {
source = xhr.responseText;
}
else if (type == FormatType.XML) {
source = xhr.responseXML;
if (!source) {
source = new DOMParser().parseFromString(xhr.responseText, 'application/xml');
}
}
else if (type == FormatType.ARRAY_BUFFER) {
source = /** @type {ArrayBuffer} */ (xhr.response);
}
if (source) {
success.call(this, format.readFeatures(source, {
extent: extent,
featureProjection: projection
}), format.readProjection(source));
}
else {
failure.call(this);
}
}
else {
failure.call(this);
}
}.bind(this);
/**
* @private
*/
xhr.onerror = function () {
failure.call(this);
}.bind(this);
xhr.send();
});
}
调试openlayers示例https://openlayers.org/en/latest/examples/select-features.html后,geojson被加载到一个名为Features的对象中,可以清楚地看到特征和属性,如图所示。
坐标是根据投影到平面做数学变换后存储的,所以,回到我最初的问题,答案是访问这个对象,有坐标但没有原始坐标,只有那些已在加载过程中处理。
再次学习 deep openlayers-6,现在我想从作为层加载的 GeoJson 文件访问几何图形,我该怎么做?在此先感谢您的帮助。
import 'ol/ol.css';
import Feature from 'ol/Feature';
import Geolocation from 'ol/Geolocation';
import Map from 'ol/Map';
import View from 'ol/View';
import Point from 'ol/geom/Point';
import TileLayer from 'ol/layer/Tile';
import VectorLayer from 'ol/layer/Vector';
import OSM from 'ol/source/OSM';
import VectorSource from 'ol/source/Vector';
import {Circle as CircleStyle, Fill, Stroke, Style} from 'ol/style';
import GeoJSON from 'ol/format/GeoJSON';
var view = new View({
center: [0, 0],
zoom: 2
});
var openstreet = new TileLayer({
source: new OSM()
});
var geozonas = new VectorLayer({
source: new VectorSource({
url:'https://geo.anantara.cl/maps/json/geozone2.json',
format: new GeoJSON()
})
});
var map = new Map({
layers: [
openstreet, geozonas
],
target: 'map',
view: view
});
继续调查,我发现了一个名为 featureloader.js 的文件,其中调用了一个读取 geojson 文件的函数,但我找不到它的存储位置,显然他并没有原封不动地保留它,而是显然进行了一种计算。您还有其他相关信息吗?
export function loadFeaturesXhr(url, format, success, failure) {
return (
/**
* @param {import("./extent.js").Extent} extent Extent.
* @param {number} resolution Resolution.
* @param {import("./proj/Projection.js").default} projection Projection.
* @this {import("./source/Vector").default|import("./VectorTile.js").default}
*/
function (extent, resolution, projection) {
var xhr = new XMLHttpRequest();
console.log(new Error().stack); //psilva
xhr.open('GET', typeof url === 'function' ? url(extent, resolution, projection) : url, true);
if (format.getType() == FormatType.ARRAY_BUFFER) {
xhr.responseType = 'arraybuffer';
}
xhr.withCredentials = withCredentials;
/**
* @param {Event} event Event.
* @private
*/
xhr.onload = function (event) {
// status will be 0 for file:// urls
if (!xhr.status || xhr.status >= 200 && xhr.status < 300) {
var type = format.getType();
/** @type {Document|Node|Object|string|undefined} */
var source = void 0;
if (type == FormatType.JSON || type == FormatType.TEXT) {
source = xhr.responseText;
}
else if (type == FormatType.XML) {
source = xhr.responseXML;
if (!source) {
source = new DOMParser().parseFromString(xhr.responseText, 'application/xml');
}
}
else if (type == FormatType.ARRAY_BUFFER) {
source = /** @type {ArrayBuffer} */ (xhr.response);
}
if (source) {
success.call(this, format.readFeatures(source, {
extent: extent,
featureProjection: projection
}), format.readProjection(source));
}
else {
failure.call(this);
}
}
else {
failure.call(this);
}
}.bind(this);
/**
* @private
*/
xhr.onerror = function () {
failure.call(this);
}.bind(this);
xhr.send();
});
}
调试openlayers示例https://openlayers.org/en/latest/examples/select-features.html后,geojson被加载到一个名为Features的对象中,可以清楚地看到特征和属性,如图所示。
坐标是根据投影到平面做数学变换后存储的,所以,回到我最初的问题,答案是访问这个对象,有坐标但没有原始坐标,只有那些已在加载过程中处理。