簡體   English   中英

我如何訪問 GeoJson 中的幾何圖形

[英]How i can access geometry inside GeoJson

在這里再次學習深度 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 文件的 function,但我找不到它的存儲位置,顯然他沒有保持完整,但顯然執行了一種計算。 你還有其他關於它的信息嗎?

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 被加載到了一個名為 ZA8CFDE6331BD59EB2ACZ6B96 的特性和特性中,然后它允許你查看特性,屬性如圖所示。

這些坐標是根據到平面的投影進行數學變換后存儲的,因此,回到我最初的問題,答案是訪問這個 object 並且有坐標但不是原始坐標,而是已經處理過的坐標在加載過程中。

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM