在 R 中绘制 native-land.ca json
Plotting native-land.ca json in R
我刚开始使用 JSON 文件,所以不确定如何处理。我正在尝试从 json 文件中绘制 native-lands.ca 中可用的土著领土。我可以将文件读取为 JSON,但它不是我可以弄清楚如何绘制的格式。
native_land_json <- jsonlite::fromJSON("https://native-land.ca/coordinates/indigenousTerritories.json")
native_land_df <- as.data.frame(native_land_json)
head(native_land_df)
type features.type features.properties.Name features.properties.FrenchName features.properties.Slug
1 FeatureCollection Feature Twana/Skokomish Twana/Skokomish twana-skokomish
2 FeatureCollection Feature Wailaki Wailaki wailaki
3 FeatureCollection Feature Yinhawangka Yinhawangka yinhawangka
4 FeatureCollection Feature Quinnipiac Quinnipiac quinnipiac
5 FeatureCollection Feature Guachichil Guachichil guachichil
6 FeatureCollection Feature Nahua (Mexico) Nahua (Mexico) nahuatl
features.properties.description features.properties.FrenchDescription
1 https://native-land.ca/maps/territories/twana-skokomish/ https://native-land.ca/maps/territories/twana-skokomish/
2 https://native-land.ca/maps/territories/wailaki/ https://native-land.ca/maps/territories/wailaki/
3 https://native-land.ca/maps/territories/yinhawangka/ https://native-land.ca/maps/territories/yinhawangka/
4 https://native-land.ca/maps/territories/quinnipiac/ https://fr.wikipedia.org/wiki/Quinnipiac
5 https://native-land.ca/maps/territories/guachichil/ https://native-land.ca/maps/territories/guachichil/
6 https://native-land.ca/maps/territories/nahuatl/ https://native-land.ca/maps/territories/nahuatl/
features.properties.color
1 #f6e810
2 #991155
3 #6211DD
4 #E822CC
5 #962222
6 #238344
features.geometry.coordinates
1 -122.90851, -122.91953, -122.93470, -122.95678, -123.01351, -123.08737, -123.15743, -123.20057, -123.24085, -123.26973, -123.31913, -123.34324, -123.33718, -123.36435, -123.41235, -123.45086, -123.49532, -123.51653, -123.48975, -123.45434, -123.43558, -123.41263, -123.39482, -123.36653, -123.32550, -123.21974, -123.11817, -123.05135, -122.92988, -122.85507, -122.81423, -122.77235, -122.75594, -122.74792, -122.72689, -122.69190, -122.66031, -122.61828, -122.56576, -122.54671, -122.54359, -122.55410, -122.59054, -122.65269, -122.71713, -122.76661, -122.82124, -122.85997, -122.88925, -122.90851, 47.85164, 47.84571, 47.83490, 47.81325, 47.78606, 47.77323, 47.76890, 47.77459, 47.79850, 47.80131, 47.78941, 47.75973, 47.69874, 47.66647, 47.61814, 47.58161, 47.55106, 47.52229, 47.47098, 47.42473, 47.39378, 47.34789, 47.31665, 47.29252, 47.27918, 47.27492, 47.28842, 47.30275, 47.35386, 47.38366, 47.42052, 47.46230, 47.51891, 47.57710, 47.64372, 47.71025, 47.75163, 47.77594, 47.80305, 47.82345, 47.84363, 47.85207, 47.85656, 47.85963, 47.86982, 47.87500, 47.87423, 47.86916, 47.85939, 47.85164
2 -123.75000, -123.77197, -123.72803, -123.66211, -123.49731, -123.38745, -123.22266, -123.07983, -123.10181, -123.12378, -123.17871, -123.31055, -123.32153, -123.44238, -123.62915, -123.75000, 40.12849, 40.10329, 39.94344, 39.94344, 39.94344, 39.91816, 39.90974, 39.94344, 40.01079, 40.11169, 40.15369, 40.15369, 40.11169, 40.14529, 40.10329, 40.12849
3 116.98242, 116.98242, 116.98242, 117.00989, 117.04834, 117.13074, 117.20215, 117.18567, 117.18018, 117.14722, 117.08130, 117.33398, 117.60864, 117.88330, 118.07556, 118.18542, 118.32275, 118.50403, 118.57544, 118.29529, 118.08655, 117.91626, 117.59216, 117.49878, 117.45483, 117.24060, 117.15271, 116.98242, -22.79644, -22.88250, -22.93310, -22.95333, -23.01908, -23.11510, -23.20601, -23.35234, -23.51363, -23.61936, -23.89588, -23.94610, -23.96116, -24.03141, -23.90091, -23.80545, -23.74010, -23.62943, -23.49851, -23.34730, -23.22620, -23.11005, -22.89768, -22.81163, -22.73059, -22.72046, -22.74579, -22.79644
4 -72.90253, -72.92725, -72.93823, -72.93274, -72.93823, -72.90527, -72.76794, -72.58118, -72.51526, -72.49878, -72.55096, -72.53723, -72.55646, -72.60590, -72.71301, -72.81738, -72.88605, -72.90253, 41.65239, 41.60518, 41.53531, 41.38505, 41.31495, 41.24684, 41.21379, 41.22205, 41.27574, 41.37062, 41.48389, 41.55587, 41.61750, 41.66881, 41.70163, 41.69137, 41.66470, 41.65239
5 -102.38978, -102.12355, -101.93277, -101.73480, -101.55193, -101.41575, -101.22391, -101.05912, -100.86960, -100.70172, -100.50465, -100.38179, -100.26130, -100.17676, -100.10250, -100.12713, -100.33064, -100.40584, -100.20166, -99.95159, -99.81282, -99.71246, -99.51542, -99.40250, -99.42627, -99.42627, -99.43690, -99.70237, -99.97558, -100.17748, -100.57983, -100.77647, -100.98962, -101.23703, -101.58016, -101.83767, -102.35462, -102.63416, -102.64369, -102.51145, -102.48146, -102.50065, -102.53817, -102.57188, -102.48460, -102.38978, 22.67024, 22.36702, 22.14000, 21.95230, 21.83441, 21.80720, 21.80870, 21.88442, 21.92599, 21.91305, 21.80208, 21.69103, 21.72460, 21.85980, 22.15700, 22.42782, 22.54598, 22.84987, 23.08585, 23.16142, 23.30944, 23.48026, 23.70853, 23.93525, 24.16680, 24.29704, 24.63912, 24.85638, 25.00597, 25.07288, 25.13534, 25.43323, 25.60484, 25.70678, 25.72492, 25.68133, 25.60952, 25.42834, 25.13629, 24.69997, 24.40117, 24.14015, 23.71418, 23.40196, 23.06784, 22.67024, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000
6 -98.91230, -98.85451, -98.76274, -98.70495, -98.68795, -98.72874, -98.77973, -98.89057, -98.99881, -99.05100, -99.15345, -99.23154, -99.26130, -99.36780, -99.45950, -99.78070, -99.91988, -100.01089, -99.99393, -99.93602, -99.78482, -99.56606, -99.42450, -99.34408, -99.32799, -99.36462, -99.32929, -99.24757, -99.18322, -99.08671, -99.03524, -98.96447, -98.95160, -98.91230, 19.29686, 19.30969, 19.34818, 19.39949, 19.51167, 19.63017, 19.73259, 19.81382, 19.85082, 19.88746, 19.90592, 19.88752, 19.86548, 19.79018, 19.75650, 19.62041, 19.44888, 19.16595, 19.07997, 19.03436, 19.00090, 18.95223, 19.00090, 19.08605, 19.17115, 19.28956, 19.38705, 19.46261, 19.58692, 19.59904, 19.53539, 19.42620, 19.33212, 19.29686
features.geometry.type features.id
1 Polygon 007d3821e00f3a136fa7285091646eff
2 Polygon 00896620a9255981e05aa70ca0e1c064
3 Polygon 00f119f67515a23509f053c1f949955e
4 Polygon 0100cd28582402da4d43dff3f6f5d337
5 Polygon 01287d68c58c5084464c96bd3664c3aa
6 Polygon 016ad4e6343ec013d0b7b7325af08fd9
文件的内容似乎是 geoJSON 格式,但我试过多个 geoJSON 包,他们似乎无法处理(甚至找不到) .
# Error in reading from URL
geojsonR::FROM_GeoJson("https://native-land.ca/coordinates/indigenousTerritories.json")
Error in export_From_geojson(url_file_string, Flatten_Coords, Average_Coordinates, :
copy into submatrix: incompatible matrix dimensions: 1x2 and 1x3
# Can't find url
geojsonio::geojson_read("https://native-land.ca/coordinates/indigenousTerritories.json")
Error: Not Found (HTTP 404)
# Error in reading saved file
geojsonio::geojson_read("indigenousTerritories.json")
Error in CPL_get_z_range(obj, 2) : z error - expecting three columns;
我在传单方面取得了一些成功,但理想情况下我想制作一张静态地图。我还想添加 json 文件中列出的地区名称和颜色。
library(leaflet)
library(magrittr)
native_land_string <- readLines("https://native-land.ca/coordinates/indigenousTerritories.json")
leaflet() %>%
setView(lng = -98.583, lat = 39.833, zoom = 3) %>%
addTiles() %>%
addGeoJSON(native_land_string)
您的错误似乎与 Github issue 中讨论的内容相似。特定数量的输入几何具有 Z 维度(并非全部)。正如您在以下输出中看到的,数组具有 2 维或 3 维,
native_land_json <- jsonlite::fromJSON("https://native-land.ca/coordinates/indigenousTerritories.json")
geoms = native_land_json$features$geometry
table(geoms$type) # all geometries are polygons
> str(geoms)
'data.frame': 1745 obs. of 2 variables:
$ coordinates:List of 1745
..$ : num [1, 1:50, 1:2] -123 -123 -123 -123 -123 ...
..$ : num [1, 1:16, 1:2] -124 -124 -124 -124 -123 ...
..$ : num [1, 1:28, 1:2] 117 117 117 117 117 ...
..$ : num [1, 1:18, 1:2] -72.9 -72.9 -72.9 -72.9 -72.9 ...
..$ : num [1, 1:46, 1:3] -102 -102 -102 -102 -102 ...
..$ : num [1, 1:34, 1:2] -98.9 -98.9 -98.8 -98.7 -98.7 ...
..$ : num [1, 1:33, 1:3] -104 -104 -104 -104 -105 ...
..$ : num [1, 1:14, 1:2] 128 128 128 128 128 ...
..$ : num [1, 1:29, 1:2] -103 -103 -104 -104 -104 ...
..$ : num [1, 1:81, 1:2] -99.4 -99.4 -99.4 -99.3 -99.3 ...
..$ : num [1, 1:29, 1:2] 137 137 137 137 137 ...
..$ : num [1, 1:25, 1:2] -71 -71 -71 -71 -71 ...
..$ : num [1, 1:75, 1:2] -122 -122 -122 -122 -122 ...
..$ : num [1, 1:32, 1:2] -65.2 -65.3 -65.5 -65.7 -65.8 ...
> geoms$coordinates[[7]]
, , 1
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16] [,17] [,18] [,19] [,20]
[1,] -104.3843 -104.3668 -104.3788 -104.4543 -104.5064 -104.6234 -104.8152 -105.0458 -105.1391 -105.2435 -105.4356 -105.5055 -105.5754 -105.6453 -105.6698 -105.6835 -105.6888 -105.4193 -105.3388 -105.3309
[,21] [,22] [,23] [,24] [,25] [,26] [,27] [,28] [,29] [,30] [,31] [,32] [,33]
[1,] -105.261 -105.111 -104.8641 -104.7477 -104.6889 -104.5842 -104.4491 -104.4032 -104.3713 -104.419 -104.4745 -104.4558 -104.3843
, , 2
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23]
[1,] 22.31837 22.41218 22.49432 22.57088 22.59706 22.62106 22.66615 22.69331 22.66257 22.61114 22.5134 22.52241 22.54695 22.62119 22.61311 22.59115 22.56763 22.20796 22.104 21.98155 21.94528 21.86844 21.8959
[,24] [,25] [,26] [,27] [,28] [,29] [,30] [,31] [,32] [,33]
[1,] 21.90905 21.84206 21.67868 21.5281 21.49923 21.52235 21.85277 22.10345 22.22072 22.31837
, , 3
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24] [,25] [,26] [,27] [,28] [,29] [,30] [,31] [,32] [,33]
[1,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
第三个维度是 Z,它是 0。您可以忽略 Z 维度并使用前 2 个继续分析,
types = geoms$type
geoms = geoms$coordinates
plgs = lapply(1:length(geoms), function(x) {
iter_type = glue::glue("{toupper(types[x])}")
iter_geom = geoms[[x]]
iter_geom = glue::glue("{iter_geom[,,1]} {iter_geom[,,2]}")
iter_geom = paste(iter_geom, collapse = ', ')
iter_out = as.character(glue::glue("{iter_type}(({iter_geom}))"))
iter_out
})
length(plgs)
我所做的是根据前两个维度创建多边形。然后你可以保留输入.json文件的属性并添加创建的多边形。
properties = native_land_json$features$properties
properties$geometry = plgs
str(properties)
基于此更新 data.frame 您可以使用 'sf' 包将数据加载为简单的特征,
df_properties = sf::st_as_sf(properties, wkt = "geometry")
然后您可以使用 'leaflet' 和 'leafgl' 包绘制 1745 个特征,
lft = leaflet::leaflet()
lft = leaflet::addProviderTiles(map = lft,
provider = leaflet::providers$Esri.WorldImagery)
lft = leafgl::addGlPolygons(map = lft,
data = df_properties,
color = 'color',
opacity = 1.0,
popup = 'Name')
lft
从输出地图的第一眼来看,在我看来,特定数量的区域重叠。一个可能对这里有帮助的函数是 sf::st_make_valid()
df_properties = sf::st_make_valid(df_properties) # check validity of created polygons
验证有效性后,您必须调整传单参数,因为输出几何图形可能已被修改。例如,一些可能已从多边形转换为多边形。
郑重声明,我是 geojsonR 包的作者,实际上是 geojsonR::FROM_GeoJson() 的作者给你的数据一个错误。
这是另一个可用于删除无效 z
值的工作流程。
- 直接使用库
geojsonsf
读取 .json
- 使用库
sfheaders
转换为 data.frame
- 使用库
sfheaders
转换回 sf
,但省略 z
列
url <- "https://native-land.ca/coordinates/indigenousTerritories.json"
library(sf)
library(sfheaders)
library(geojsonsf)
sf <- geojsonsf::geojson_sf(url)
df <- sfheaders::sf_to_df(sf, fill = TRUE)
## All of the non-NA z values are 0
## you can ignore these if you want and reconstruct the sf object without the 'z' component
unique( df[ !is.na(df$z), "z" ] )
sf_new <- sfheaders::sf_polygon(
obj = df
, x = "x"
, y = "y"
, polygon_id = "polygon_id"
, linestring_id = "linestring_id"
, keep = TRUE
)
sf::st_crs( sf_new ) <- sf::st_crs( sf )
和情节...
library(mapdeck)
set_token("YOUR_MAPBOX_API_TOKEN")
mapdeck() %>%
add_polygon(
data = sf_new
, fill_colour = "Name"
)
我刚开始使用 JSON 文件,所以不确定如何处理。我正在尝试从 json 文件中绘制 native-lands.ca 中可用的土著领土。我可以将文件读取为 JSON,但它不是我可以弄清楚如何绘制的格式。
native_land_json <- jsonlite::fromJSON("https://native-land.ca/coordinates/indigenousTerritories.json")
native_land_df <- as.data.frame(native_land_json)
head(native_land_df)
type features.type features.properties.Name features.properties.FrenchName features.properties.Slug
1 FeatureCollection Feature Twana/Skokomish Twana/Skokomish twana-skokomish
2 FeatureCollection Feature Wailaki Wailaki wailaki
3 FeatureCollection Feature Yinhawangka Yinhawangka yinhawangka
4 FeatureCollection Feature Quinnipiac Quinnipiac quinnipiac
5 FeatureCollection Feature Guachichil Guachichil guachichil
6 FeatureCollection Feature Nahua (Mexico) Nahua (Mexico) nahuatl
features.properties.description features.properties.FrenchDescription
1 https://native-land.ca/maps/territories/twana-skokomish/ https://native-land.ca/maps/territories/twana-skokomish/
2 https://native-land.ca/maps/territories/wailaki/ https://native-land.ca/maps/territories/wailaki/
3 https://native-land.ca/maps/territories/yinhawangka/ https://native-land.ca/maps/territories/yinhawangka/
4 https://native-land.ca/maps/territories/quinnipiac/ https://fr.wikipedia.org/wiki/Quinnipiac
5 https://native-land.ca/maps/territories/guachichil/ https://native-land.ca/maps/territories/guachichil/
6 https://native-land.ca/maps/territories/nahuatl/ https://native-land.ca/maps/territories/nahuatl/
features.properties.color
1 #f6e810
2 #991155
3 #6211DD
4 #E822CC
5 #962222
6 #238344
features.geometry.coordinates
1 -122.90851, -122.91953, -122.93470, -122.95678, -123.01351, -123.08737, -123.15743, -123.20057, -123.24085, -123.26973, -123.31913, -123.34324, -123.33718, -123.36435, -123.41235, -123.45086, -123.49532, -123.51653, -123.48975, -123.45434, -123.43558, -123.41263, -123.39482, -123.36653, -123.32550, -123.21974, -123.11817, -123.05135, -122.92988, -122.85507, -122.81423, -122.77235, -122.75594, -122.74792, -122.72689, -122.69190, -122.66031, -122.61828, -122.56576, -122.54671, -122.54359, -122.55410, -122.59054, -122.65269, -122.71713, -122.76661, -122.82124, -122.85997, -122.88925, -122.90851, 47.85164, 47.84571, 47.83490, 47.81325, 47.78606, 47.77323, 47.76890, 47.77459, 47.79850, 47.80131, 47.78941, 47.75973, 47.69874, 47.66647, 47.61814, 47.58161, 47.55106, 47.52229, 47.47098, 47.42473, 47.39378, 47.34789, 47.31665, 47.29252, 47.27918, 47.27492, 47.28842, 47.30275, 47.35386, 47.38366, 47.42052, 47.46230, 47.51891, 47.57710, 47.64372, 47.71025, 47.75163, 47.77594, 47.80305, 47.82345, 47.84363, 47.85207, 47.85656, 47.85963, 47.86982, 47.87500, 47.87423, 47.86916, 47.85939, 47.85164
2 -123.75000, -123.77197, -123.72803, -123.66211, -123.49731, -123.38745, -123.22266, -123.07983, -123.10181, -123.12378, -123.17871, -123.31055, -123.32153, -123.44238, -123.62915, -123.75000, 40.12849, 40.10329, 39.94344, 39.94344, 39.94344, 39.91816, 39.90974, 39.94344, 40.01079, 40.11169, 40.15369, 40.15369, 40.11169, 40.14529, 40.10329, 40.12849
3 116.98242, 116.98242, 116.98242, 117.00989, 117.04834, 117.13074, 117.20215, 117.18567, 117.18018, 117.14722, 117.08130, 117.33398, 117.60864, 117.88330, 118.07556, 118.18542, 118.32275, 118.50403, 118.57544, 118.29529, 118.08655, 117.91626, 117.59216, 117.49878, 117.45483, 117.24060, 117.15271, 116.98242, -22.79644, -22.88250, -22.93310, -22.95333, -23.01908, -23.11510, -23.20601, -23.35234, -23.51363, -23.61936, -23.89588, -23.94610, -23.96116, -24.03141, -23.90091, -23.80545, -23.74010, -23.62943, -23.49851, -23.34730, -23.22620, -23.11005, -22.89768, -22.81163, -22.73059, -22.72046, -22.74579, -22.79644
4 -72.90253, -72.92725, -72.93823, -72.93274, -72.93823, -72.90527, -72.76794, -72.58118, -72.51526, -72.49878, -72.55096, -72.53723, -72.55646, -72.60590, -72.71301, -72.81738, -72.88605, -72.90253, 41.65239, 41.60518, 41.53531, 41.38505, 41.31495, 41.24684, 41.21379, 41.22205, 41.27574, 41.37062, 41.48389, 41.55587, 41.61750, 41.66881, 41.70163, 41.69137, 41.66470, 41.65239
5 -102.38978, -102.12355, -101.93277, -101.73480, -101.55193, -101.41575, -101.22391, -101.05912, -100.86960, -100.70172, -100.50465, -100.38179, -100.26130, -100.17676, -100.10250, -100.12713, -100.33064, -100.40584, -100.20166, -99.95159, -99.81282, -99.71246, -99.51542, -99.40250, -99.42627, -99.42627, -99.43690, -99.70237, -99.97558, -100.17748, -100.57983, -100.77647, -100.98962, -101.23703, -101.58016, -101.83767, -102.35462, -102.63416, -102.64369, -102.51145, -102.48146, -102.50065, -102.53817, -102.57188, -102.48460, -102.38978, 22.67024, 22.36702, 22.14000, 21.95230, 21.83441, 21.80720, 21.80870, 21.88442, 21.92599, 21.91305, 21.80208, 21.69103, 21.72460, 21.85980, 22.15700, 22.42782, 22.54598, 22.84987, 23.08585, 23.16142, 23.30944, 23.48026, 23.70853, 23.93525, 24.16680, 24.29704, 24.63912, 24.85638, 25.00597, 25.07288, 25.13534, 25.43323, 25.60484, 25.70678, 25.72492, 25.68133, 25.60952, 25.42834, 25.13629, 24.69997, 24.40117, 24.14015, 23.71418, 23.40196, 23.06784, 22.67024, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000
6 -98.91230, -98.85451, -98.76274, -98.70495, -98.68795, -98.72874, -98.77973, -98.89057, -98.99881, -99.05100, -99.15345, -99.23154, -99.26130, -99.36780, -99.45950, -99.78070, -99.91988, -100.01089, -99.99393, -99.93602, -99.78482, -99.56606, -99.42450, -99.34408, -99.32799, -99.36462, -99.32929, -99.24757, -99.18322, -99.08671, -99.03524, -98.96447, -98.95160, -98.91230, 19.29686, 19.30969, 19.34818, 19.39949, 19.51167, 19.63017, 19.73259, 19.81382, 19.85082, 19.88746, 19.90592, 19.88752, 19.86548, 19.79018, 19.75650, 19.62041, 19.44888, 19.16595, 19.07997, 19.03436, 19.00090, 18.95223, 19.00090, 19.08605, 19.17115, 19.28956, 19.38705, 19.46261, 19.58692, 19.59904, 19.53539, 19.42620, 19.33212, 19.29686
features.geometry.type features.id
1 Polygon 007d3821e00f3a136fa7285091646eff
2 Polygon 00896620a9255981e05aa70ca0e1c064
3 Polygon 00f119f67515a23509f053c1f949955e
4 Polygon 0100cd28582402da4d43dff3f6f5d337
5 Polygon 01287d68c58c5084464c96bd3664c3aa
6 Polygon 016ad4e6343ec013d0b7b7325af08fd9
文件的内容似乎是 geoJSON 格式,但我试过多个 geoJSON 包,他们似乎无法处理(甚至找不到) .
# Error in reading from URL
geojsonR::FROM_GeoJson("https://native-land.ca/coordinates/indigenousTerritories.json")
Error in export_From_geojson(url_file_string, Flatten_Coords, Average_Coordinates, :
copy into submatrix: incompatible matrix dimensions: 1x2 and 1x3
# Can't find url
geojsonio::geojson_read("https://native-land.ca/coordinates/indigenousTerritories.json")
Error: Not Found (HTTP 404)
# Error in reading saved file
geojsonio::geojson_read("indigenousTerritories.json")
Error in CPL_get_z_range(obj, 2) : z error - expecting three columns;
我在传单方面取得了一些成功,但理想情况下我想制作一张静态地图。我还想添加 json 文件中列出的地区名称和颜色。
library(leaflet)
library(magrittr)
native_land_string <- readLines("https://native-land.ca/coordinates/indigenousTerritories.json")
leaflet() %>%
setView(lng = -98.583, lat = 39.833, zoom = 3) %>%
addTiles() %>%
addGeoJSON(native_land_string)
您的错误似乎与 Github issue 中讨论的内容相似。特定数量的输入几何具有 Z 维度(并非全部)。正如您在以下输出中看到的,数组具有 2 维或 3 维,
native_land_json <- jsonlite::fromJSON("https://native-land.ca/coordinates/indigenousTerritories.json")
geoms = native_land_json$features$geometry
table(geoms$type) # all geometries are polygons
> str(geoms)
'data.frame': 1745 obs. of 2 variables:
$ coordinates:List of 1745
..$ : num [1, 1:50, 1:2] -123 -123 -123 -123 -123 ...
..$ : num [1, 1:16, 1:2] -124 -124 -124 -124 -123 ...
..$ : num [1, 1:28, 1:2] 117 117 117 117 117 ...
..$ : num [1, 1:18, 1:2] -72.9 -72.9 -72.9 -72.9 -72.9 ...
..$ : num [1, 1:46, 1:3] -102 -102 -102 -102 -102 ...
..$ : num [1, 1:34, 1:2] -98.9 -98.9 -98.8 -98.7 -98.7 ...
..$ : num [1, 1:33, 1:3] -104 -104 -104 -104 -105 ...
..$ : num [1, 1:14, 1:2] 128 128 128 128 128 ...
..$ : num [1, 1:29, 1:2] -103 -103 -104 -104 -104 ...
..$ : num [1, 1:81, 1:2] -99.4 -99.4 -99.4 -99.3 -99.3 ...
..$ : num [1, 1:29, 1:2] 137 137 137 137 137 ...
..$ : num [1, 1:25, 1:2] -71 -71 -71 -71 -71 ...
..$ : num [1, 1:75, 1:2] -122 -122 -122 -122 -122 ...
..$ : num [1, 1:32, 1:2] -65.2 -65.3 -65.5 -65.7 -65.8 ...
> geoms$coordinates[[7]]
, , 1
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16] [,17] [,18] [,19] [,20]
[1,] -104.3843 -104.3668 -104.3788 -104.4543 -104.5064 -104.6234 -104.8152 -105.0458 -105.1391 -105.2435 -105.4356 -105.5055 -105.5754 -105.6453 -105.6698 -105.6835 -105.6888 -105.4193 -105.3388 -105.3309
[,21] [,22] [,23] [,24] [,25] [,26] [,27] [,28] [,29] [,30] [,31] [,32] [,33]
[1,] -105.261 -105.111 -104.8641 -104.7477 -104.6889 -104.5842 -104.4491 -104.4032 -104.3713 -104.419 -104.4745 -104.4558 -104.3843
, , 2
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23]
[1,] 22.31837 22.41218 22.49432 22.57088 22.59706 22.62106 22.66615 22.69331 22.66257 22.61114 22.5134 22.52241 22.54695 22.62119 22.61311 22.59115 22.56763 22.20796 22.104 21.98155 21.94528 21.86844 21.8959
[,24] [,25] [,26] [,27] [,28] [,29] [,30] [,31] [,32] [,33]
[1,] 21.90905 21.84206 21.67868 21.5281 21.49923 21.52235 21.85277 22.10345 22.22072 22.31837
, , 3
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24] [,25] [,26] [,27] [,28] [,29] [,30] [,31] [,32] [,33]
[1,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
第三个维度是 Z,它是 0。您可以忽略 Z 维度并使用前 2 个继续分析,
types = geoms$type
geoms = geoms$coordinates
plgs = lapply(1:length(geoms), function(x) {
iter_type = glue::glue("{toupper(types[x])}")
iter_geom = geoms[[x]]
iter_geom = glue::glue("{iter_geom[,,1]} {iter_geom[,,2]}")
iter_geom = paste(iter_geom, collapse = ', ')
iter_out = as.character(glue::glue("{iter_type}(({iter_geom}))"))
iter_out
})
length(plgs)
我所做的是根据前两个维度创建多边形。然后你可以保留输入.json文件的属性并添加创建的多边形。
properties = native_land_json$features$properties
properties$geometry = plgs
str(properties)
基于此更新 data.frame 您可以使用 'sf' 包将数据加载为简单的特征,
df_properties = sf::st_as_sf(properties, wkt = "geometry")
然后您可以使用 'leaflet' 和 'leafgl' 包绘制 1745 个特征,
lft = leaflet::leaflet()
lft = leaflet::addProviderTiles(map = lft,
provider = leaflet::providers$Esri.WorldImagery)
lft = leafgl::addGlPolygons(map = lft,
data = df_properties,
color = 'color',
opacity = 1.0,
popup = 'Name')
lft
从输出地图的第一眼来看,在我看来,特定数量的区域重叠。一个可能对这里有帮助的函数是 sf::st_make_valid()
df_properties = sf::st_make_valid(df_properties) # check validity of created polygons
验证有效性后,您必须调整传单参数,因为输出几何图形可能已被修改。例如,一些可能已从多边形转换为多边形。
郑重声明,我是 geojsonR 包的作者,实际上是 geojsonR::FROM_GeoJson() 的作者给你的数据一个错误。
这是另一个可用于删除无效 z
值的工作流程。
- 直接使用库
geojsonsf
读取 .json
- 使用库
sfheaders
转换为 - 使用库
sfheaders
转换回sf
,但省略z
列
data.frame
url <- "https://native-land.ca/coordinates/indigenousTerritories.json"
library(sf)
library(sfheaders)
library(geojsonsf)
sf <- geojsonsf::geojson_sf(url)
df <- sfheaders::sf_to_df(sf, fill = TRUE)
## All of the non-NA z values are 0
## you can ignore these if you want and reconstruct the sf object without the 'z' component
unique( df[ !is.na(df$z), "z" ] )
sf_new <- sfheaders::sf_polygon(
obj = df
, x = "x"
, y = "y"
, polygon_id = "polygon_id"
, linestring_id = "linestring_id"
, keep = TRUE
)
sf::st_crs( sf_new ) <- sf::st_crs( sf )
和情节...
library(mapdeck)
set_token("YOUR_MAPBOX_API_TOKEN")
mapdeck() %>%
add_polygon(
data = sf_new
, fill_colour = "Name"
)