簡體   English   中英

將 GeoJson 多邊形坐標轉換為 lat / lng

[英]Convert GeoJson polygon coordinates to lat / lng

我只是想問你,是否有任何方法可以將 GeoJson 多邊形坐標轉換為 lat 和 lng。 我在網上沒有找到任何有用的東西。

這是來自 GeoJson 文件的片段

{ 
  "type": "Feature", 
  "properties": { "Id": 0 }, 
  "geometry": { 
    "type": "Polygon", 
    "coordinates": [ 
      [ -526814.91964827105, -1166902.422062967 ], 
      [ -526808.96392099187, -1166933.277827289 ], 
      [ -526824.83895273879, -1166938.0403368101 ], 
      ...
    ]
  } 
},

我沒有找到將這些數字轉換為 lat / lng 的任何公式或方法。 如果您有這方面的經驗,請告訴我!

謝謝你。

我已經測試了這個 function 在 JavaScript 中用於多邊形和多多邊形。 它應該可以擴展以覆蓋更簡單的幾何圖形,例如點和線串。 我希望它可以幫助你:

/* convert polygons from lng/lat to lat/lng. we need to get the number of geometries 
contained in the territory object. note that a single polygon may have more than one 
geometry if it has a hole in it. a multiPolygon should always have more than one 
geometry because they represent polygons */
function swapCoordinates(GeoJSON) {
  for (var i = 0; i < GeoJSON.geometry.coordinates.length; i++) {
    /* if this is a polygon, length is the number of co-ordinates for this polygon. if 
    this is a multiPolygon, length is the number of geometries in this polygon */
    if (type == "Polygon") {
      // iterate over this polygon's co-ordinates
      for (var j = 0; j < GeoJSON.geometry.coordinates[i].length; j++) {
        if (!paths) {
          paths = [];
        }

        if (!paths[i]) {
          paths[i] = [];
        }

        paths[i].push({
          lat: GeoJSON.geometry.coordinates[i][j][1],
          lng: GeoJSON.geometry.coordinates[i][j][0]
        });
      }
    } else if (type == "MultiPolygon") {
      // for multiPolygons, GeoJSON.geometry.coordinates.length represents the number of polygons, so we need to go one level deeper to get the number of geometries
      objectPolygon = []; // reset the polygon object

      // iterate over this multiPolygon's polygons
      for (var j = 0; j < GeoJSON.geometry.coordinates[i].length; j++) {
        innerCoords = []; // reset the inner coordinates
        length = GeoJSON.geometry.coordinates[i][j].length; // get the number of co-ordinates for this polygon
        clockwise = turf.booleanClockwise(GeoJSON.geometry.coordinates[i][j]); // check the co-ordinates winding. clockwise is a normal polygon, counter-clockwise is a hole

        // if this polygon's co-ordinates winding is counter-clockwise, this is a multiPolygon with holes so it needs to be handled differently
        if (!clockwise) {
          holes = true;
        }

        // iterate over this polygon's co-ordinates
        for (var k = 0; k < length; k++) {
          coordinates = {
            lat: GeoJSON.geometry.coordinates[i][j][k][1],
            lng: GeoJSON.geometry.coordinates[i][j][k][0]
          };

          // push the polygon geometry into objectPolygon
          if (clockwise) {
            objectPolygon.push(coordinates);
          }

          // push the holes into innerCoords
          else {
            innerCoords.push(coordinates);
          }
        }
      }

      if (!paths) {
        paths = [];
      }

      if (holes) {
        paths.push([objectPolygon, innerCoords]);
      } else {
        paths.push(objectPolygon);
      }
    }
  }

  return paths;
}

假設您有一個包含以下數據的變量feature

var feature = { 
  "type": "Feature", 
  "properties": { "Id": 0 }, 
  "geometry": { 
    "type": "Polygon", 
    "coordinates": [ 
      [ -526814.91964827105, -1166902.422062967 ], 
      [ -526808.96392099187, -1166933.277827289 ], 
      [ -526824.83895273879, -1166938.0403368101 ]
    ]
  } 
};

要將其轉換為具有 lat/lng 格式坐標的數組,您需要做的就是:

var coordinates = feature.geometry.coordinates.map(v => ({lat: v[0], lng: v[1]}));

如果您需要在末尾復制第一個坐標,只需添加以下行:

coordinates.push(coordinates[0]);

演示

 var feature = { "type": "Feature", "properties": { "Id": 0 }, "geometry": { "type": "Polygon", "coordinates": [ [ -526814.91964827105, -1166902.422062967 ], [ -526808.96392099187, -1166933.277827289 ], [ -526824.83895273879, -1166938.0403368101 ] ] } }; var coordinates = feature.geometry.coordinates.map(v => ({lat: v[0], lng: v[1]})); coordinates.push(coordinates[0]); console.log(coordinates);

暫無
暫無

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

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