简体   繁体   English

开放层GeoJson中的指数坐标表示法

[英]Exponential coordinate notation in Open Layers GeoJson

I'm trying to draw a line using Open Layers and I sight the example here - https://openlayers.org/en/latest/examples/geojson.html . 我正在尝试使用开放层画一条线,并且在这里看到了示例-https: //openlayers.org/en/latest/examples/geojson.html

{
   'type': 'Feature',
   'geometry': {
   'type': 'LineString',
   'coordinates': [[4e6, -2e6], [8e6, 2e6]]
 }

The confusion here is that how is regular lat lon coordinates converted into this exponential notation. 这里的困惑在于规则纬度坐标如何转换为该指数符号。 If I use regular lat lon, nothing is drawn on the map? 如果我使用常规纬度,那么地图上没有任何内容吗?

{
   'type': 'Feature',
   'geometry': {
   'type': 'LineString',
   'coordinates': [[42.12154, -20.14515], [80.7845241, 20.4545741]]
 }

How do I convert my coordinates to that exponential notation? 如何将坐标转换为指数表示法?

Is there a way to draw geojson in OL using the regular coordinate system? 有没有一种方法可以使用常规坐标系在OL中绘制geojson?

These coordinates are not in lat-long but in 3857. 这些坐标不是经纬度,而是3857。

To use lat-long, you would need to change the Json declaration, by removing the CRS attribute 要使用lat-long,您需要通过删除CRS属性来更改Json声明

var geojsonObject = {
        'type': 'FeatureCollection',
        'crs': {
          'type': 'name',
          'properties': {
            'name': 'EPSG:3857'
          }
        }, ...

You can study this example and the accompanying GeoJson . 您可以研究此示例以及随附的GeoJson

To use latitude/longitude in your GeoJSON, you need to add the correct projections to the GeoJSON format: 要在GeoJSON中使用经度/纬度,您需要向GeoJSON格式添加正确的投影:

  var vectorSource = new ol.source.Vector({ // VectorSource({
    features: (new ol.format.GeoJSON({
      featureProjection: 'EPSG:3857',
      dataProjection: 'EPSG:4326'})).readFeatures(geojsonObject)
  });

The OSM map tiles use EPSG:3857, the GeoJSON uses EPSG:4326 (which is the default). OSM地图图块使用EPSG:3857,GeoJSON使用EPSG:4326(默认设置)。

proof of concept 概念证明

生成的地图的屏幕截图

code snippet: 代码段:

 var styles = { 'LineString': new ol.style.Style({ stroke: new ol.style.Stroke({ color: 'green', width: 2 }) }) }; var styleFunction = function(feature) { return styles[feature.getGeometry().getType()]; }; var geojsonObject = { 'type': 'FeatureCollection', 'features': [{ 'type': 'Feature', 'geometry': { 'type': 'LineString', 'coordinates': [[42.12154, -20.14515], [80.7845241, 20.4545741]] } }] }; var vectorSource = new ol.source.Vector({ // VectorSource({ features: (new ol.format.GeoJSON({ featureProjection: 'EPSG:3857', dataProjection: 'EPSG:4326' })).readFeatures(geojsonObject) }); var vectorLayer = new ol.layer.Vector({ // VectorLayer({ source: vectorSource, style: styleFunction }); var map = new ol.Map({ layers: [ new ol.layer.Tile({ // TileLayer({ source: new ol.source.OSM() }), vectorLayer ], target: 'map', controls: ol.control.defaults({ // defaultControls({ attributionOptions: { collapsible: false } }), view: new ol.View({ center: [0, 0], zoom: 2 }) }); 
 html, body, .map { height: 100%; width: 100%; padding: 0px; margin: 0px; } 
 <link rel="stylesheet" href="https://openlayers.org/en/v5.1.3/css/ol.css" type="text/css"> <script src="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.1.3/build/ol.js"></script> <div id="map" class="map"></div> 

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

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