使用javascript生成GeoJson

时间:2015-06-01 17:32:36

标签: javascript php ajax json geojson

我正在使用ajax

获取lat / lon数组
$.ajax({
    type: "POST",
    url: '../m/m_share.php',
    data: 'zone=' + zone,
    dataType: 'json',
    success: function(tab) {
        var i = 0;
        var size = tab.length;
        for (i = 0; i < size; i++) {
            var lat = tab[i]['lat'];
            var lon = tab[i]['lon'];
        }
    }

“tab”参数是由我发送的来自db的lat / lon请求的php发送的jscon_encode(数组)。 我想做的是创建一个像这样的geojson但是使用我的lat / lon数据。

var geojson =
   {"name":"NewFeatureType",
    "type":"FeatureCollection",
    "features":[{"type":"Feature",
                 "geometry":{"type":"LineString",
                             "coordinates":[[169.13693,-44.696476,296],[169.134602,-44.69764,295],[169.129983,-44.701164,299]]},
    "properties":null}]};

我试图将lat / lon保存在var

  $.ajax({
            type: "POST",
            url: '../m/m_share.php',
            data: 'zone=' + zone,
            dataType: 'json',
            success: function(tab) {
                var i = 0;
                var size = tab.length;
                for (i = 0; i < size; i++) {
                     var lat = tab[i]['lat'];
                     var lon = tab[i]['lon'];
                   if(i===size){
                      coord = coord+'['+lat+','+lon+']';
                       alert(coord);
                    }
                    else{
                      coord = coord+'['+lat+','+lon+']'+',';

                    }
                }
                }
            });

然后用我的coord var替换geoJson中的lat / lon,但似乎传单不喜欢它“无效的LatLng对象:( NaN,Nan)”。

2 个答案:

答案 0 :(得分:5)

创建一个geojson对象变量。

import numpy as np
import itertools

slices = (slice(None, -1, None), slice(1, None, None))

def Expectation(values, numeraire, i, i0=0):
    def Values(values, i):
        factors = values.ndim
        expect = np.zeros((i,)*factors)
        for j in itertools.product(slices, repeat=factors):
            expect += values[j]
        return expect*0.5**factors*numeraire(i, i-1)
    return reduce(Values, range(i, i0, -1), values)

class Numeraire:
    def __init__(self, factors, rate=0):
        self.factors = factors
        self.rate = rate
    def __call__(self, timenext, time):
        return np.full((time+1,)*factors, np.exp(-self.rate*(timenext - time)))

factors = 2
i = 360
values, numeraire = np.ones((i+1,)*factors), Numeraire(factors, 0.05/12)
%timeit Expectation(values, numeraire, i)
Expectation(values, numeraire, i)[(0,)*factors], np.exp(-0.05/12*i)

然后你推进坐标数组

var geojson = {
    "name":"NewFeatureType",
    "type":"FeatureCollection",
    "features":[{
        "type":"Feature",
        "geometry":{
            "type":"LineString",
            "coordinates":[]
        },
        "properties":null
    }]
};

答案 1 :(得分:1)

Pedro Estrada提到的方法是正确的。但是,需要进行一些更正。

GeoJson标准要求具有(经度,纬度)约定的地理位置。

var gj = {
    "name":"MyFeatureType",
    "type":"FeatureCollection",
    "features":[]
};

推送一个新的特征对象

gj.features.push({ "type": "Feature","geometry": {"type": "LineString","coordinates": []},"properties": null });

向新推送的对象添加坐标:

lon=20;
lat=10;
gj.features[0].geometry.coordinates.push([lon,lat]);