使用 Leaflet 显示 geojson featureCollection
Show geojson featureCollection with Leaflet
我使用 QGIS 导出了一个多边形图层作为 geojson,我想用传单发布它。这是 geojson 的样子 [ 由于 SO 字符限制 ] 而被排除:
https://gist.github.com/t-book/88806d12d7f05024b147715be82e6844
这是我试过的:
包装的 geojson 为 var:
var states = [{
"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::31468" } },
"features": [
{ "type": "Feature", "properties": ...
}];
添加为新图层:
L.geoJSON(states, {
style: function(feature) {
switch (feature.properties.party) {
case 'Euerbach': return {color: "#ff0000"};
case 'Werneck': return {color: "#0000ff"};
}
}
}).addTo(map);
很遗憾,没有呈现任何内容。如何将此 geojson featureCollection 正确添加到我的地图?
问题是您的数据是投影的 - Leaflet 期望您的数据未投影(由 long/lat 对组成,或 WGS84/EPSG 4326 中的 "projected")。有几个解决方案,这里想到两个:
在 QGIS 中,导出数据,使其由 long/lat 个坐标对组成
在显示 geojson 时使用 proj4.js 重新投影您的坐标。
对于第二个,您需要在将 geojson 添加为图层时设置 coordsToLatLng 选项:
var geojson = L.geoJSON(states, {
coordsToLatLng: function (p) {
// return get lat/lng point here.
})
此函数的主体将使用 proj4 在 geojson 的坐标参考系统 (CRS) 中获取坐标,并在 WGS84 中获取坐标 return。
此外,coordsToLatLng 函数要求您 return lat/long 对。由于您的 geojson 和 proj4 表示 [x,y] 的数据,我们需要在 returning 新点之前交换我们的值。
这可能看起来像:
var geojson = L.geoJSON(states, {
coordsToLatLng: function (p) {
p = proj4(fromProjection,toProjection,p); // reproject each point
p = [p[1],p[0]] // swap the values
return p; // return the lat/lng pair
}
}).addTo(map);
当然,我们需要定义我们的 CRS。我在 spatialreference.org 上查找了您的 CRS(它在 geojson 本身中指定)并使用为该 CRS 和 EPSG4326 (WGS84) 提供的描述来设置我的 fromProjection 和 toPojection:
var fromProjection = '+proj=tmerc +lat_0=0 +lon_0=12 +k=1 +x_0=4500000 +y_0=0 +ellps=bessel +datum=potsdam +units=m +no_defs ';
var toProjection = "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs ";
总而言之,这给了我们一些东西 like this。
请记住,如果您有大文件,在 javascript 中重新投影它们将比在适当的 CRS 中导出它们花费更长的时间。
我使用 QGIS 导出了一个多边形图层作为 geojson,我想用传单发布它。这是 geojson 的样子 [ 由于 SO 字符限制 ] 而被排除: https://gist.github.com/t-book/88806d12d7f05024b147715be82e6844
这是我试过的:
包装的 geojson 为 var:
var states = [{
"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::31468" } },
"features": [
{ "type": "Feature", "properties": ...
}];
添加为新图层:
L.geoJSON(states, {
style: function(feature) {
switch (feature.properties.party) {
case 'Euerbach': return {color: "#ff0000"};
case 'Werneck': return {color: "#0000ff"};
}
}
}).addTo(map);
很遗憾,没有呈现任何内容。如何将此 geojson featureCollection 正确添加到我的地图?
问题是您的数据是投影的 - Leaflet 期望您的数据未投影(由 long/lat 对组成,或 WGS84/EPSG 4326 中的 "projected")。有几个解决方案,这里想到两个:
在 QGIS 中,导出数据,使其由 long/lat 个坐标对组成
在显示 geojson 时使用 proj4.js 重新投影您的坐标。
对于第二个,您需要在将 geojson 添加为图层时设置 coordsToLatLng 选项:
var geojson = L.geoJSON(states, {
coordsToLatLng: function (p) {
// return get lat/lng point here.
})
此函数的主体将使用 proj4 在 geojson 的坐标参考系统 (CRS) 中获取坐标,并在 WGS84 中获取坐标 return。
此外,coordsToLatLng 函数要求您 return lat/long 对。由于您的 geojson 和 proj4 表示 [x,y] 的数据,我们需要在 returning 新点之前交换我们的值。
这可能看起来像:
var geojson = L.geoJSON(states, {
coordsToLatLng: function (p) {
p = proj4(fromProjection,toProjection,p); // reproject each point
p = [p[1],p[0]] // swap the values
return p; // return the lat/lng pair
}
}).addTo(map);
当然,我们需要定义我们的 CRS。我在 spatialreference.org 上查找了您的 CRS(它在 geojson 本身中指定)并使用为该 CRS 和 EPSG4326 (WGS84) 提供的描述来设置我的 fromProjection 和 toPojection:
var fromProjection = '+proj=tmerc +lat_0=0 +lon_0=12 +k=1 +x_0=4500000 +y_0=0 +ellps=bessel +datum=potsdam +units=m +no_defs ';
var toProjection = "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs ";
总而言之,这给了我们一些东西 like this。 请记住,如果您有大文件,在 javascript 中重新投影它们将比在适当的 CRS 中导出它们花费更长的时间。