标签: shapefile

使用 Cartopy 和 Geopandas 遮罩形状文件外部的区域

我有一组用 Cartopy 绘制的数据(经度、纬度、温度)。我可以给出的最小示例是下面的代码(只有 30 个数据点)

import cartopy.crs as ccrs
import cartopy.feature as cfeature
import matplotlib.pyplot as plt
import matplotlib.colors as clr
import pandas as pd
import numpy as np

from metpy.interpolate import interpolate_to_grid, remove_nan_observations
from cartopy.io.shapereader import Reader
from cartopy.feature import ShapelyFeature

canada_east = -95
canada_west = -101.8
canada_north = 52.8
canada_south = 48.85

central_lon = (canada_east + canada_west)/2
central_lat = (canada_north + canada_south)/2

crs = ccrs.LambertConformal(central_longitude = central_lon, central_latitude = central_lat)
lat = np.array([49.8134 50.904  50.698  49.095  49.436  49.9607 …
Run Code Online (Sandbox Code Playgroud)

python colors shapefile cartopy geopandas

1
推荐指数
1
解决办法
1458
查看次数

如何在球形文件上正确绘制坐标?

所以我试图在地球上绘制一堆坐标并跟踪每个国家有多少个坐标。我已经很好地绘制了地图和坐标,但是当我尝试使用交集来计算每个国家(多边形)内有多少个坐标时,会导致错误。我尝试使用 st_make_valid 函数来修复地球形状文件,但它弄乱了几何形状。我是 R 的新手,因此我们将不胜感激。

我使用以下代码来绘制地球形状文件和顶部的坐标:

library(tidyverse)
library(sf)
library(rmapshaper)
library(rnaturalearth)
library(rnaturalearthdata)
library(sp)
library(raster)

###############

# Load Data

###############

# Read in data from .csv file

MeteoriteData <- read.csv("C:/Users/ChaseDickson_/Desktop/College/AERO 689/Semester Project/Meteorite Landings.csv")

# Convert these points to an SF object, specifying the X and Y

#  column names, and supplying the CRS as 4326 (which is WGS84)

MeteoriteData.sf <- st_as_sf(MeteoriteData, coords=c('long', 'lat'), crs=4326)

world <- (ne_countries(scale = "medium", returnclass = "sf"))

MeteoriteMap <- ggplot(data = world) +
  geom_sf() +
  geom_sf(data = …
Run Code Online (Sandbox Code Playgroud)

r geospatial shapefile r-sf

1
推荐指数
1
解决办法
175
查看次数

将R中目录中的所有shapefile读入内存

我想将目录中的所有 shapefile 读入全局环境但是,当我在工作目录中获取文件列表时,该列表包含 .shp 和 .shp.xml 文件,后者是我不想要的。我的草稿代码读取 .shp 和 .shp.xml 文件。我怎样才能阻止它这样做?

草案代码如下:

库(地图工具)

# get all files with the .shp extension from working directory
setwd("N:/Dropbox/_BonesFirst/139_Transit_Metros_Points_Subset_by_R")
shps <- dir(getwd(), "*.shp")
# the assign function will take the string representing shp
# and turn it into a variable which holds the spatial points data 
for (shp in shps) {
  cat("now loading", shp, "...", '\n\r')
  assign(shp, readOGR(shp)) 
                  }
Run Code Online (Sandbox Code Playgroud)

编辑:问题似乎出在 readShapePoints 中。readOGR(来自 rgdal)或 shapefile(来自 raster)效果更好。

for-loop r shapefile

0
推荐指数
1
解决办法
5377
查看次数

两个 shapefile 相交后:`vapply(g2, st_is_empty, logical(1)) 中的错误`

我运行以下命令:

library(dplyr)
library(sf)
library(tigris)
library(tmap)

options(tigris_class = 'sf')
options(tigris_use_cache = TRUE)

nj = tigris::states(cb = T, year = 2015) %>%
  filter(STUSPS == 'NJ')

nj_msas = tigris::core_based_statistical_areas(cb = T, year = 2015) %>%
  filter(grepl('NJ', NAME)) %>%
  sf::st_intersection(nj)

tmap_mode('plot')

nj_msas %>%
  tm_shape() +
  tm_polygons()
Run Code Online (Sandbox Code Playgroud)

最后一个块给出了错误:

vapply(g2, st_is_empty, logical(1)) 中的错误:值长度必须为 1,但 FUN(X[[3]]) 结果长度为 148

当我删除 时st_intersection,最后一个块没有错误。我无法通过谷歌搜索在任何地方找到此错误消息。有谁知道发生了什么?

另外,如果我运行除最后一个块之外的所有上述内容,并且我ggplot2::geom_sf用来创建地图而不是tmap函数,我会得到我想要的地图。没有错误。

我正在使用 Ubuntu 18.04.1。RStudio v1.1.463。R 3.5.2。底格里斯河 0.7。地图 2.2。平方英尺 0.7-2。

r shapefile tigris tmap r-sf

0
推荐指数
1
解决办法
388
查看次数

向 GeoDataFrame 添加新列,当它匹配时,它说值的长度与索引不匹配

目标是读取 shapefile,获取每个多边形(城市)的面积,并将该面积添加到名为“面积”的新列中。

cs = gpd.read_file('munis.shp')
area_fd = []
area = cs['geometry'].map(lambda p: p.area / 10**6)
area_fd.append(area)
cs = cs.assign(Area =  area_fd) #or cs['Area'] = area_fd
Run Code Online (Sandbox Code Playgroud)

当我运行时,print(cs['NAME_2'].head)它返回 38 munis (0-38) 并print(area_fd)给出 38 个值。但是当我运行代码时,它说ValueError: Length of values does not match length of index。我已经查找了类似的 GISSE 和 SO 线程,但没有解决方案有效。

indexing shapefile python-3.x pandas geopandas

0
推荐指数
1
解决办法
3865
查看次数