简体   繁体   中英

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.

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. 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.

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.

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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