geojson圈子,支持与否?

时间:2013-06-05 14:32:48

标签: maps d3.js leaflet geojson

当我查看GeoJson的规格时,我看到圈子得到支持:

http://geopriv.dreamhosters.com/geojson/geojson-spec.html#circleExample

但是,当我尝试使用geojsonlint(http://geojsonlint.com/)中的代码时,它会给我一个错误。

输入:

{ 
"type": "Circle",
"coordinates": [4.884, 52.353],
"radius": 200
}

给予:

"Circle" is not a valid GeoJSON type. 

我希望通过使用d3显示不同的兴趣点,并对地图产生一系列影响。它需要GeoJson作为输入,但是GeoJson不支持圆圈吗?

5 个答案:

答案 0 :(得分:24)

  

当我查看GeoJson的规格时,我看到支持圈子

他们不是。似乎你设法找到一些假的或不正确的规格。转到geojson.org查找specs,没有关于圈子的信息。

答案 1 :(得分:6)

正如accepted answer所解释的那样,GeoJson规范不支持圆圈。

然而,大多数地图实现都会让您创建圆圈,但是如果这对您不起作用(例如,您需要将其存储在数据库中并进行查询),解决方案是创建一个多边形粗略地近似一个圆圈(想象一个有32个边缘的多边形)。

我写了module that does this。您可以像这样使用它:

const circleToPolygon = require('circle-to-polygon');

const coordinates = [-27.4575887, -58.99029]; //[lon, lat]
const radius = 100;                           // in meters
const numberOfEdges = 32;                     //optional that defaults to 32

let polygon = circleToPolygon(coordinates, radius, numberOfEdges);

为了澄清为什么不支持圆圈,它与地球的曲率有关。因为地球不是一个完美的球体,如果你要在它上画圆形,它也不是一个完美的圆形。但是,上述解决方案适用于您不需要严格精确度的大多数情况。

答案 2 :(得分:4)

geojson没有圈子支持, 但您可以使用LineString来模拟圆圈

使用LineString geiometry对象

"geometry": {
        "type": "LineString",
        "coordinates": [
                    [center_X, center_y],
                    [center_X, center_y]
                ]
      }
then set the dynamic style use radius as the strokeweight
function featureStyle(feature){
    return {
      strokeWeight: radius,
    };
  }

它在地图上看起来像一个圆圈。

答案 3 :(得分:0)

A circle... some code I use for making a circle for an OpenStreetMap

-- x is decimal latitude
-- y is decimal longitude
-- r is radius --  .00010 is about 40m in OSM (3 about 50km)

-- Adjust for map latitude distortion further north or south

x = math.log(math.tan((90 + x) * math.pi/360)) / (math.pi/180)

-- For loop to gather all the points of circle here 1 to 360
-- can also use the for loop to make some other interesting shapes

for i = 1, 360 do
    angle = i * math.pi / 180
    ptx = x + r * math.cos( angle )
    pty = y + r * math.sin( angle )

-- readjust latitude for map distortion

    ptx = 180/math.pi * (2 * math.atan(math.exp( ptx * math.pi/180)) - math.pi/2 )

-- Build an array of positions for GeoJSON - formatted lat and long

    data[i] = '[' .. string.format("%.6f",pty) .. ","
    data[i] = data[i] .. string.format("%.6f",ptx) .. ']'

-- End of for loop
end

-- Cycle through the data array with another for loop to build the
   coordinates (put in brackets and commas etc. for the actual
   GeoJSON coordinates string. Add data[1] to the end to close
   the polygon (circle). A circle.

-- If you want a solid circle then use fill and a hex color
-- If you want a LineString just make fill invisible or #ffffff
      Include the stroke-width and stroke-color parameters as well.
-- If latitude is greater than 89.5 or less than -89.5 you may wish
   to cut off the circle by drawing a line at polar regions by using
   those latitudes.
-- I use this simply for several circles and not for hundreds of them.

Cheers!
-- 

答案 4 :(得分:-1)

使用要素属性存储有关要素的其他信息。