Google maps 将多段线转换为GeoJSON

Google maps 将多段线转换为GeoJSON,google-maps,maps,geojson,Google Maps,Maps,Geojson,我有一系列(编码或解码的)多段线,它们是从谷歌地图/开放街道地图等服务中捕获的 例如: var polylines = ["kclaG|i_qLe@i@{AgBu@_AsCyCiBmBMMW[eCiC}A_BEEeBiB{@}@MW]a@a@e@]a@IGGG_AgAm@i@MOYYMEQSCCi@k@KKu@y@{A}Ay@cA{@{@eBiBOMe@k@_@a@e@e@QQY[cAkAUWMOUWu@w@AACCYY?AMKUUSSSQ]]GGECCCECA?AAA?A?C?G?WB"

我有一系列(编码或解码的)多段线,它们是从谷歌地图/开放街道地图等服务中捕获的

例如:

var polylines = ["kclaG|i_qLe@i@{AgBu@_AsCyCiBmBMMW[eCiC}A_BEEeBiB{@}@MW]a@a@e@]a@IGGG_AgAm@i@MOYYMEQSCCi@k@KKu@y@{A}Ay@cA{@{@eBiBOMe@k@_@a@e@e@QQY[cAkAUWMOUWu@w@AACCYY?AMKUUSSSQ]]GGECCCECA?AAA?A?C?G?WB"
       ,"yfnaGld}pL?e@?]AuDAgA?KAkBGqG?{C?_B?S?aC?[?]A}A?wAASAQ"
       ,"qmnaGlxxpLn@tEL~@"]
我想将它们存储为GeoJSON。我已经找到了很多通过诸如传单之类的工具呈现GeoJSON的包,但是我很难找到可以将多段线导出到GeoJSON的包

是否存在类似的解决方案,或者是否存在GeoJSON标准的特定子集,以便我可以编写工具myset


旁注:我最终想要一个KML或SHP文件。我计划使用将GeoJSON转换为SHP。

您正在寻找GeoJSON LineString几何体类型。虽然算法有点复杂,但是有一个脚本提供了JavaScript的功能。您应该能够轻松地将其移植到您选择的语言中。我还将该功能包装到JSFIDLE中,以便您可以将其用作在线工具

您将在找到该工具-您可以将编码的多段线粘贴到“编码的多段线”中,然后单击“解码”,这将返回一个GeoJSON LineString。由于StackOverflow的规则要求JSFIDLE附带代码,因此代码在下面被复制,但要在Github项目站点获取最新的代码

function decode() {
  var encodedPolyline = document.getElementById("encoded-polyline").value;
  document.getElementById("geojson").value = encodedPolyline;
  var coords = polyline.toGeoJSON(encodedPolyline);
  document.getElementById("geojson").value = JSON.stringify(coords);
}

var polyline = {};

function encode(current, previous, factor) {
  current = Math.round(current * factor);
  previous = Math.round(previous * factor);
  var coordinate = current - previous;
  coordinate <<= 1;
  if (current - previous < 0) {
    coordinate = ~coordinate;
  }
  var output = '';
  while (coordinate >= 0x20) {
    output += String.fromCharCode((0x20 | (coordinate & 0x1f)) + 63);
    coordinate >>= 5;
  }
  output += String.fromCharCode(coordinate + 63);
  return output;
}

/**
 * Decodes to a [latitude, longitude] coordinates array.
 *
 * This is adapted from the implementation in Project-OSRM.
 *
 * @param {String} str
 * @param {Number} precision
 * @returns {Array}
 *
 * @see https://github.com/Project-OSRM/osrm-frontend/blob/master/WebContent/routing/OSRM.RoutingGeometry.js
 */
polyline.decode = function(str, precision) {
  var index = 0,
    lat = 0,
    lng = 0,
    coordinates = [],
    shift = 0,
    result = 0,
    byte = null,
    latitude_change,
    longitude_change,
    factor = Math.pow(10, precision || 5);

  // Coordinates have variable length when encoded, so just keep
  // track of whether we've hit the end of the string. In each
  // loop iteration, a single coordinate is decoded.
  while (index < str.length) {

    // Reset shift, result, and byte
    byte = null;
    shift = 0;
    result = 0;

    do {
      byte = str.charCodeAt(index++) - 63;
      result |= (byte & 0x1f) << shift;
      shift += 5;
    } while (byte >= 0x20);

    latitude_change = ((result & 1) ? ~(result >> 1) : (result >> 1));

    shift = result = 0;

    do {
      byte = str.charCodeAt(index++) - 63;
      result |= (byte & 0x1f) << shift;
      shift += 5;
    } while (byte >= 0x20);

    longitude_change = ((result & 1) ? ~(result >> 1) : (result >> 1));

    lat += latitude_change;
    lng += longitude_change;

    coordinates.push([lat / factor, lng / factor]);
  }

  return coordinates;
};

/**
 * Encodes the given [latitude, longitude] coordinates array.
 *
 * @param {Array.<Array.<Number>>} coordinates
 * @param {Number} precision
 * @returns {String}
 */
polyline.encode = function(coordinates, precision) {
  if (!coordinates.length) {
    return '';
  }

  var factor = Math.pow(10, precision || 5),
    output = encode(coordinates[0][0], 0, factor) + encode(coordinates[0][1], 0, factor);

  for (var i = 1; i < coordinates.length; i++) {
    var a = coordinates[i],
      b = coordinates[i - 1];
    output += encode(a[0], b[0], factor);
    output += encode(a[1], b[1], factor);
  }

  return output;
};

function flipped(coords) {
  var flipped = [];
  for (var i = 0; i < coords.length; i++) {
    flipped.push(coords[i].slice().reverse());
  }
  return flipped;
}

/**
 * Encodes a GeoJSON LineString feature/geometry.
 *
 * @param {Object} geojson
 * @param {Number} precision
 * @returns {String}
 */
polyline.fromGeoJSON = function(geojson, precision) {
  if (geojson && geojson.type === 'Feature') {
    geojson = geojson.geometry;
  }
  if (!geojson || geojson.type !== 'LineString') {
    throw new Error('Input must be a GeoJSON LineString');
  }
  return polyline.encode(flipped(geojson.coordinates), precision);
};

/**
 * Decodes to a GeoJSON LineString geometry.
 *
 * @param {String} str
 * @param {Number} precision
 * @returns {Object}
 */
polyline.toGeoJSON = function(str, precision) {
  var coords = polyline.decode(str, precision);
  return {
    type: 'LineString',
    coordinates: flipped(coords)
  };
};

if (typeof module === 'object' && module.exports) {
  module.exports = polyline;
}
函数解码(){
var encodedPolyline=document.getElementById(“编码多段线”).value;
document.getElementById(“geojson”).value=encodedPolyline;
var coords=polyline.toGeoJSON(encodedPolyline);
document.getElementById(“geojson”).value=JSON.stringify(coords);
}
var polyline={};
功能编码(当前、先前、因子){
当前=数学四舍五入(当前*系数);
上一次=数学四舍五入(上一次*系数);
var坐标=当前-先前;
坐标>=5;
}
输出+=String.fromCharCode(坐标+63);
返回输出;
}
/**
*解码为[纬度,经度]坐标数组。
*
*这是根据OSRM项目中的实现进行调整的。
*
*@param{String}str
*@param{Number}精度
*@returns{Array}
*
*@见https://github.com/Project-OSRM/osrm-frontend/blob/master/WebContent/routing/OSRM.RoutingGeometry.js
*/
polyline.decode=函数(str,精度){
var指数=0,
lat=0,
液化天然气=0,
坐标=[],
移位=0,
结果=0,
字节=空,
纬度变化,
经度改变,
系数=数学功率(10,精度| | 5);
//坐标在编码时具有可变长度,所以只需保留
//跟踪我们是否已到达字符串的末尾。在每个
//循环迭代,解码单个坐标。
while(索引>1):(结果>>1));
移位=结果=0;
做{
byte=str.charCodeAt(index++)-63;
结果|=(字节&0x1f)=0x20);
经度变化=((结果&1)~(结果>>1):(结果>>1));
lat+=纬度变化;
lng+=经度变化;
坐标。推力([lat/系数,lng/系数]);
}
返回坐标;
};
/**
*对给定的[纬度,经度]坐标数组进行编码。
*
*@param{Array.}坐标
*@param{Number}精度
*@返回{String}
*/
polyline.encode=函数(坐标、精度){
如果(!坐标.长度){
返回“”;
}
var系数=数学功率(10,精度| | 5),
输出=编码(坐标[0][0],0,因子)+编码(坐标[0][1],0,因子);
对于(变量i=1;i
当您说“我捕获了一系列多段线”时,多段线的捕获格式是什么?@geocodezip我现在有json格式的编码形式,尽管我已经在通过传单加载和渲染它们,所以我可以访问组成它们的点。为什么您需要转换为GeoJSON?您可能会感兴趣的是,哪些将编码多段线添加到地图和博览会中将它们命名为KML。