Openlayers 提供的 API 讀取、解析和展示 GeoJSON 描述的信息


 

 

 

 

Openlayers 提供的 API 讀取、解析和展示 GeoJSON 描述的信息。

1. 官網介紹

 

GeoJSON is a format for encoding a variety of geographic data structures.

 

{
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [125.6, 10.1]
      },
      "properties": {
        "name": "Dinagat Islands"
      }
    }
}

 

 

GeoJSON supports the following geometry types: Point, LineString, Polygon, MultiPoint, MultiLineString, and MultiPolygon. Geometric objects with additional properties are Feature objects. Sets of features are contained by FeatureCollection objects.

 

2. GeoJSON 重要對象成員(名/值對)。

字段 是否必須 可選范圍
type 必須 Point
MultiPoint
LineString
MultiLineString
Polygon
MultiPolygon
GeometryCollection
Feature
FeatureCollection
crs 可選 必須是一個坐標參考系統的對象
bbox 可選 必須是邊界框數組

 

 二、OpenLayers 使用 GeoJSON

 

官方示例: https://openlayers.org/en/latest/examples/geojson.html

 

1. 定義樣式和獲取樣式的方法

    var styles = {
        'Polygon': new Style({
            stroke: new Stroke({
              color: 'blue',
              lineDash: [4],
              width: 3,
            }),
            fill: new Fill({
              color: 'rgba(0, 0, 255, 0.1)',
            }),
      })
    };
    
    var styleFunction = function (feature) {
      return styles[feature.getGeometry().getType()];
    };

2. 解析並展示 GeoJSON

    // GeoJson 對象示例
    var geojsonObject = {
      type: "Feature",
      geometry: {
        type: "Polygon",
        coordinates: [
          [
            [1e6, -3e6],
            [2e6, -4e6],
            [3e6, -6e6]
          ]
        ]
      }
    };
    
    // 將 geoJSON 解析成 feature 並添加到 vectorLayer 數據源中    
    var vectorSource = new VectorSource({
       features: new GeoJSON().readFeatures(geojsonObject)
    });
    
    //使用數據源和顯示方法構建圖層
    var vectorLayer = new VectorLayer({
      source: vectorSource,
      style: styleFunction
    });

也可以在創建完成后調用 API 動態添加 feature:

this.vectorSource.addFeatures(features);

VectorSource API : https://openlayers.org/en/latest/apidoc/module-ol_source_Vector-VectorSource.html

也支持ARCGIS的Json格式

var vectorSource = new VectorSource({
    features: new ol.format.EsriJSON().readFeatures(geojsonObject)
})

 

EsriJSON

EsriJSON 是一種用於對各種地理數據結構進行編碼的格式。對於 EsriJSON,其通常是指 FeatureSet 對象,其中 FeatureSet 包含一組 Feature 對象。在 ArcGIS Velocity 中,可以將 EsriJSON 提取為 FeatureSet 對象(要素集合)或者將單個 Feature 對象提取為行。

https://doc.arcgis.com/zh-cn/iot/ingest/esrijson.htm

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM