简体   繁体   English

我如何访问 GeoJson 中的几何图形

[英]How i can access geometry inside GeoJson

Here again studying in deep openlayers-6, now i would like to access geometry from GeoJson file loaded as layer, how i can do it?, thanks in advance for your help.在这里再次学习深度 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
});

Continuing with the investigation, I've found a file called featureloader.js, where the call to a function that reads the geojson file is made, but I can't find where it stores it, and apparently he does not keep it intact, but performs a kind of calculation apparently.继续调查,我发现了一个名为 featureloader.js 的文件,其中调用了读取 geojson 文件的 function,但我找不到它的存储位置,显然他没有保持完整,但显然执行了一种计算。 Do you have any other information about it?你还有其他关于它的信息吗?

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();
});

} }

After debugging the openlayers example https://openlayers.org/en/latest/examples/select-features.html , it is clear and convincing that the geojson is loaded into an object called Features, then it allows you to view the characteristics and properties as shown in the image.在调试了 openlayers 示例https://openlayers.org/en/latest/examples/select-features.html 之后,很清楚且令人信服的是,geojson 被加载到了一个名为 ZA8CFDE6331BD59EB2ACZ6B96 的特性和特性中,然后它允许你查看特性,属性如图所示。

The coordinates are stored after these are made mathematical transformations according to the projection to the plane, therefore, returning to my initial question, the answer is to access this object and there are the coordinates but not the original ones but those that have already been processed in the loading process.这些坐标是根据到平面的投影进行数学变换后存储的,因此,回到我最初的问题,答案是访问这个 object 并且有坐标但不是原始坐标,而是已经处理过的坐标在加载过程中。

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM