sf

convert a fortified data.frame back to sf object

試著忘記壹切 提交于 2019-12-10 19:17:27
问题 fiftystater package provides a great map of USA that has Hawaii and Alaska as insets below. The object fifty_states comes already fortified for use with ggplot2. However, I would like to plot this as an sf object using geom_sf. As a more general question, what is the best way to convert a fortified data.frame back into sf polygon? library(fiftystater) fifty_states <– fifty_states > head(fifty_states) long lat order hole piece id group 1 -85.07007 31.98070 1 FALSE 1 alabama Alabama.1 2 -85

Do the values returned by rgeos::gCentroid() and sf::st_centroid() differ?

萝らか妹 提交于 2019-12-10 19:07:08
问题 Question Do the values returned by rgeos::gCentroid() and sf::st_centroid() differ? If so, how? Context After reading the relevant commands exported by rgeos section within the r-spatial/sf wiki, I was thrilled to see that I only needed the sf package - and no longer needed to import the rgeos package - to calculate the centroid of a given geometry. However, the use of sf::st_centroid() gave me this warning, which is addressed here: Warning message: In st_centroid.sfc(x = comarea606$geometry)

How to select certain geometries from a geometrycollection after st_intersect?

家住魔仙堡 提交于 2019-12-10 17:13:59
问题 I am running an intersect of two polygons or other sf objects using the fantastic new sf package. It's similar to this: a <- st_polygon(list(cbind(c(0,0,7.5,7.5,0),c(0,-1,-1,0,0)))) b <- st_polygon(list(cbind(c(0,1,2,3,4,5,6,7,7,0),c(1,0,.5,0,0,0.5,-0.5,-0.5,1,1)))) i <- st_intersection(a,b) ## GEOMETRYCOLLECTION(POINT(1 0), LINESTRING(4 0, 3 0), POLYGON((5.5 0, 7 0, 7 -0.5, 6 -0.5, 5.5 0))) how do I only keep the POLYGON of the GEOMETRYCOLLECTION ? Selecting different types in a feature

Set the right crs on sf object to plot coordinate points

最后都变了- 提交于 2019-12-10 15:10:06
问题 I'm trying to define the right CRS for my sf object. I want to plot points atop the following layer (country: the Netherlands): Simple feature collection with 380 features and 3 fields geometry type: MULTIPOLYGON dimension: XY bbox: xmin: 13565.4 ymin: 306846.2 xmax: 278026.1 ymax: 619232.6 epsg (SRID): NA proj4string: +proj=sterea +lat_0=52.15616055555555 +lon_0=5.38763888888889 +k=0.9999079 +x_0=155000 +y_0=463000 +ellps=bessel +units=m +no_defs output: This layer has the correct projection

R - Cloropleth: Of the data points that fall within a polygon, what percentage have a specific column value?

£可爱£侵袭症+ 提交于 2019-12-08 04:53:07
问题 An extension of this thread: Create choropleth map from coordinate points. (I didn't want to combine the two threads for the sake of being relevant to as many people as possible.) I have a data frame consisting of many observations, each with geocoordinates (latitude-longitude) and a boolean (yes-no) value. I would like to generate a choropleth map of the world where each region/polygon is shaded by the percentage of points within it where the associated boolean value is equal to true. Here

Add multiple legends to ggplot2 when using geom_sf

断了今生、忘了曾经 提交于 2019-12-07 19:31:32
问题 My question combines two separate issues posted on before on Stackoverflow: i. Adding multiple legends to ggplot and ii. Add line legend to geom_sf. I would like to add multiple legends to ggplot2 (as in the first post), but am using sf . This complicates filling up the aesthetic space. The answer suggested in i. above does not work well with multiple types of geometries -- we cannot assign points and lines to a single class and then use factors. In my case, I have several line and point

How to do a “full” union with the R package sf

巧了我就是萌 提交于 2019-12-07 16:13:54
问题 I try to do a union between three polygons using sf::st_union . In the figure below showing the result from ArcGIS "Overlay, Union, All" I wish to obtain a similar result as the five different polygons in 'OUTPUT' by using the sf package in R. library(sf) a1 <- st_polygon(list(rbind(c(0, 10), c(45, 10), c(45, 90), c(0, 90), c(0, 10)))) a2 <- st_polygon(list(rbind(c(45, 10), c(90,10), c(90, 90), c(45, 90), c(45, 10)))) b <- st_polygon(list(rbind(c(15, 5), c(75, 5), c(75, 50), c(15, 50), c(15,

Add multiple legends to ggplot2 when using geom_sf

て烟熏妆下的殇ゞ 提交于 2019-12-06 15:54:17
My question combines two separate issues posted on before on Stackoverflow: i. Adding multiple legends to ggplot and ii. Add line legend to geom_sf . I would like to add multiple legends to ggplot2 (as in the first post), but am using sf . This complicates filling up the aesthetic space. The answer suggested in i. above does not work well with multiple types of geometries -- we cannot assign points and lines to a single class and then use factors. In my case, I have several line and point shapefiles, and simply want to add a separate legend entry for each shapefile added. There seems to be no

Fastest way to determine COUNTRY from millions of GPS coordinates [R]

给你一囗甜甜゛ 提交于 2019-12-06 06:41:07
问题 I have millions of GPS coordinates and want to quickly add a column of the country of the coordinates. My current method works but is extremely slow: library(data.table) #REPRODUCE DATA data <- data.table(latitude=sample(seq(47,52,by=0.001), 1000000, replace = TRUE), longitude=sample(seq(8,23,by=0.001), 1000000, replace = TRUE)) #REQUIRED PACKAGES if (!require("sp")) install.packages("sp") if (!require("rworldmap")) install.packages("rworldmap") if (!require("sf")) install.packages("sf")

How can I speed up spatial operations in `dplyr::mutate()`?

蹲街弑〆低调 提交于 2019-12-05 08:17:10
I am working on a spatial problem using the sf package in conjunction with dplyr and purrr . I would prefer to perform spatial operations inside a mutate call, like so: simple_feature %>% mutate(geometry_area = map_dbl(geometry, ~ as.double(st_area(.x)))) I like that this approach allows me to run a series of spatial operations using %>% and mutate . I dislike that this approach seems to significantly increase the run-time of the sf functions (sometimes prohibitively) and I would appreciate hearing suggestions about how to overcome this speed loss. Here is a reprex that illustrates the speed