当我查找 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 确实不支持圆圈吗?
当我查找 GeoJson 的规格时,我发现支持圆圈
他们不是。看来您找到了一些假的或不正确的规格。去 geojson.org 找到 specs,没有关于圆的内容。
正如接受的答案所解释的那样,GeoJson 规范不支持圆圈。
但是,大多数地图实现都会让您创建圆形,但如果这对您不起作用(例如,您需要将其存储在数据库中并查询它),解决方案是创建一个大致近似于的多边形圆(想象一个具有 32 条以上边的多边形)。
我编写了一个模块来执行此操作。你可以这样使用它:
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);
要澄清为什么不支持圆,这与地球的曲率有关。因为地球不是一个完美的球体,如果你在上面画一个圆形,它也不是一个完美的圆形。但是,上述解决方案适用于大多数不需要严格精度的场景。
geojson 没有圆支持, 但你可以使用 LineString 来模拟圆
使用 LineString 几何对象
"geometry": {
"type": "LineString",
"coordinates": [
[center_X, center_y],
[center_X, center_y]
]
}
然后设置动态样式使用半径作为描边权重
function featureStyle(feature){
return {
strokeWeight: radius,
};
}
它在地图上看起来像一个圆圈。
对于osm静态地图,只需在template.html中添加一段代码即可:
pointToLayer: function (feature, latlng) {
if (feature.markerIconOptions) {
return L.marker(latlng, {icon: L.icon(feature.markerIconOptions)})
}
if (feature.properties.radius) {
return new L.Circle(latlng, feature.properties.radius);
}
return L.marker(latlng, {icon: myIcon});
}
示例:http://localhost:3000/dynamic?geojson=%5B%7B%0A%20%20%20%20%22type%22%3A%20%22Feature%22%2C%0A%20%20% 20%20%22属性%22%3A%20%7B%0A%20%20%20%20%20%20%22半径%22%3A%20500%0A%20%20%20%20%7D%2C% 0A%20%20%20%20%22几何%22%3A%20%7B%20%22类型%22%3A%20%22点%22%2C%20%22坐标%22%3A%20%5B16.359459% 2C%2048.203747%5D%20%7D%0A%7D%2C%0A%7B%0A%20%20%20%20%22类型%22%3A%20%22功能%22%2C%0A%20%20% 20%20%22属性%22%3A%20%7B%0A%20%20%20%20%20%20%22半径%22%3A%20500%0A%20%20%20%20%7D%2C% 0A%20%20%20%20%22几何%22%3A%20%7B%20%22类型%22%3A%20%22点%22%2C%20%22坐标%22%3A%20%5B16.359969% 2C%2048.203850%5D%20%7D%0A%7D%5D
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!
--