Questions tagged [r]

Statistical computing language and software environment.

Filter by
Sorted by
Tagged with
75 votes
6 answers
226k views

Opening shapefile in R? [closed]

I need to open a shapefile from ArcMap in R to use it for further geostatistical analysis. I've converted it into ASCII text file, but in R it is recognized as data.frame. Coordinates function doesn't ...
  • 751
72 votes
5 answers
43k views

Extracting data.frame from simple features object in R

Is there an sf-native (i.e. "correct") way of extracting everything except the geometry column from a simple features object? This works df <- dplyr::select(as.data.frame(sf), -geometry) but the ...
51 votes
3 answers
71k views

How to calculate polygon centroids in R (for non-contiguous shapes)

I've spent a little while figuring out the answer to this question. It's not immediately obvious from a Google search, so thought it may useful to post the answer on here. There is also an additional ...
46 votes
4 answers
44k views

Reading KML file into R?

I'm working with huge .kml files (up to 10 Gb) and need an efficient way to read them into R. Until now I've been converting them to shapefiles via QGIS and then back into R with readShapePoly and ...
44 votes
10 answers
10k views

Seeking spatial R tricks

R is becoming pretty strong tool for handling and analyzing spatial data. I learned some useful things through questions like these at SO and thought it might be useful to have something simmilar, but ...
44 votes
2 answers
51k views

How can I convert data in the form of lat, lon, value into a raster file using R?

I have a data set of values over a km grid in the continental U.S. The columns are "latitude", "longitude", and "observation", e.g.: "lat" "lon" "yield" 25.567 -120.347 3.6 25.832 -120....
  • 1,883
43 votes
2 answers
69k views

Projecting sp objects in R

I have a number of shapefiles in different CRSs (mostly WGS84 lat/lon) that I'd like to transform into a common projection (likely Albers Equal Area Conic, but I may ask for help on choosing in ...
43 votes
3 answers
33k views

Seeking tutorials, books, papers, blogs, etc to handle spatial data in R

Does anyone know some good tutorials, books, papers, blogs, etc to handle spatial data in R? I know that there are some packages like rgdal and maptools but I haven't actually found useful tutorials ...
42 votes
5 answers
34k views

Fixing non-noded intersection problem using PostGIS

I'm using a PL/R function and PostGIS to generate voronoi polygons around a set of points. The function that I am using is defined here. When I use this function on a particular dataset I get the ...
  • 16k
42 votes
2 answers
37k views

R convert sf object back to SpatialPolygonsDataFrame

How do I convert an sf object back to a SpatialPolygonsDataFrame? I am running R 3.4.0 and sf version 0.4.1. The sf documentation mentions an as_Spatial function, but this isn't available in my ...
  • 765
41 votes
5 answers
36k views

Making beautiful maps in R? [closed]

There are quite some nice possibilities to analyse spatial data in R and in the context of my current project I would like to use R more frequently to do that. Until now I plot my maps with ggplot2 ...
  • 1,391
39 votes
7 answers
51k views

Clustering spatial data in R? [closed]

I have bunch of data points with latitude and longitude. I want to use R to cluster them based on their distance. I have already taken a look at this page and tried clustTool package. But I am not ...
  • 491
39 votes
6 answers
27k views

Increasing speed of crop, mask, & extract raster by many polygons in R?

I'm extracting the area and percent cover of different land use types from a raster based on several thousand polygon boundaries. I've found that the extract function works much faster if I iterate ...
38 votes
4 answers
31k views

Overlaying spatial polygon with grid and checking in which grid element specific coordinates are located using R [closed]

How can one use R to split a shapefile in 200 meter squares/sub-polygons, plot this grid (incl. ID numbers for each square) over the original map below, and evaluate in which square specific ...
  • 391
37 votes
2 answers
80k views

Clipping raster in R

I am building a map for the northeastern U.S.A. The map background needs to be either an altitude map or a mean annual temperature map. I have two rasters from Worldclim.org which give me these ...
37 votes
2 answers
32k views

Reading feature class in file geodatabase using R?

I have a feature contained in a geodatabase that is larger than 2GB as an exported shapefile. I need to run an extract function in R to attribute the polygons with data from a raster file. Exporting ...
37 votes
1 answer
26k views

Lon-Lat to Simple Features (sfg and sfc) in R

How can I convert lon-lat points to simple features (sfg), and then put them in a simple feature collection (sfc)? Here is a MWE that doesn't work but is the closest I have gotten to. library(data....
  • 824
36 votes
1 answer
36k views

Getting TopologyException: Input geom 1 is invalid which is due to self-intersection in R?

The 'TopologyException: Input geom 1 is invalid' self-intersection error which arises from invalid polygon geometries has been widely discussed. However, I haven't found a convenient solution on the ...
  • 5,083
35 votes
3 answers
94k views

Finding centroid of cluster of points using R

While searching the web, solutions for finding centroids of polygons come up rather often. What I'm interested in is finding a centroid of a cluster of points. A weighted mean of sorts. Can you ...
33 votes
2 answers
56k views

Joining polygons in R

I am wondering how to join spatial polygons using R code? I'm working with census data where certain areas change over time and I wish to join the polygons and the corresponding data and simply ...
33 votes
2 answers
7k views

How can I conduct Geographically Weighted Principal Component Analysis using ArcGIS, Python and SPSS/R?

I am after a description/methodology for conducting a Geographically Weighted Principal Components Analysis (GWPCA). I am happy using Python for any portion of this and I imagine SPSS or R being used ...
33 votes
4 answers
18k views

How to perform Random Forest land cover classification?

This is a follow-up to a previous post: Machine Learning Algorithms for Land Cover Classification. It seems that the Random Forest (RF) classification method is gaining much momentum in the remote ...
  • 50.5k
33 votes
1 answer
40k views

Raster legend in R: how to colour specific values?

I have an elevation model plotted in R r <- raster("example.dem") plot(r, col = topo.colors(20)) Some of the values are below sea level (0), and I'd like to have those display in red. How can I ...
  • 14.5k
29 votes
9 answers
18k views

Working with PostGIS data in R?

I work with R almost all the time, and now I am using it for doing spatial data mining. I have a PostGIS database with (obviously) GIS data. If I want to make statistical spatial analysis and plot ...
29 votes
6 answers
26k views

Read a table from an ESRI file geodatabase (.gdb) using R

I am trying to read a table directly from an ESRI file geodatabase into R. An example data file can be downloaded here. The database contains a point feature class (Zone9_2014_01_Broadcast) and two ...
29 votes
1 answer
76k views

How do I change the polygon fill color and border color for SpatialPolygons objects?

I have SpatialPolygons or SpatialPolygonsDataFrames which I'd like to plot. How do I change the color using the three plotting systems in R (base graphics, lattice graphics, and ggplot2)? Example ...
29 votes
7 answers
8k views

Speed of various raster data formats

I'm having trouble locating any discussion or comparative benchmarking of different raster file formats (e.g., for use in data analysis in R). Does anybody have any insight into why particular formats ...
  • 291
28 votes
2 answers
56k views

Extracting intersection areas in R

I have two polygons. One contains fields(X,Y,Z) and the other contains soil types (A,B,C,D). I want to know what area of every field contains which type of soil. I tried the following: library(rgdal)...
28 votes
3 answers
49k views

Join spatial point data to polygons in R

I am trying to perform a spatial join between point data and polygon data. I have data that indicate the spatial coordinates of an event in my csv file A and have another file, shapefile B, that ...
  • 383
27 votes
4 answers
19k views

Crop simple features object in R

Is there a function to crop sf map object, similar to maptools::pruneMap(lines, xlim= c(4, 10), ylim= c(10, 15)) used for SpatialPolygon or SpatialLine? I am considering st_intersection() but there ...
  • 29.3k
25 votes
3 answers
43k views

Merging multiple SpatialPolygonDataFrames into 1 SPDF in R?

I have created 2 polygons in QGIS. Using them in R, polygons become automatically SpatialPolygonsDataFrame (SPDF). I would like to merge them into single SPDF (as is super easy in ArcGis using Tool ...
  • 3,276
25 votes
4 answers
5k views

How can I generate irregular grid containing minimum n points?

Given a large (~ 1 million) sample of unevenly distributed points - is it possible to generate irregular grid (in size, but could also be irregular in shape if that is possible?) that will contain ...
  • 10.4k
25 votes
1 answer
55k views

How to assign CRS to RasterLayer in R

I have a layer with coord. ref. : NA. How can I assign coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 to coord. ref. : NA? class : RasterLayer dimensions : 127,...
  • 821
25 votes
3 answers
15k views

How to speed up the plotting of polygons in R?

I want to plot the country borders of North America over a raster image depicting some variable and then overlay contours on top of the plot using R. I have been successful in doing this using base ...
  • 816
24 votes
1 answer
14k views

Simplify polygons of sf object

How do I simplify an sf polygon without introducing gaps and slivers? With a shapefile, for example, I would use rmapshaper::ms_simplify(): library("pryr") library("rgdal") library("rmapshaper") ...
  • 1,109
23 votes
2 answers
12k views

equivalent of sp package's point in polygon / overing using sf

I am migrating code from sp package to the newer sf package. My previous code I had a polygon SpatialDataFrame (censimentoMap) and a SpatialPointDataFrame (indirizzi.sp) and I got the polygon cell id (...
22 votes
4 answers
22k views

Proper way to rbind SpatialPolygonsDataFrames with identical polygon IDs?

What's the proper R idiom for rbinding SPDFs together when the IDs overlap? Note that here (as is often the case) the IDs are basically meaningless so it's pretty annoying that I can't just make ...
22 votes
3 answers
17k views

How to Find the Average Raster Value of an area defined by a shapefile using R?

I have a set of raster images which represent a specific month over the years, and I want to make a timeline of the average values of an area using a shapefile. How do I extract the values from the ...
  • 11.1k
22 votes
2 answers
16k views

Instantiating spatial polygon without using a shapefile in R

So, the usual way we read a shapefile in R is via the maptools package, like this: sfdata <- readShapeSpatial("/path/to/my/shapefile.shp", proj4string=CRS("+proj=longlat")) However, I have a use ...
22 votes
5 answers
37k views

How to convert a spatialpolygon to a SpatialPolygonsDataFrame and add a column to the attribute table

coast<-readShapeSpatial("coastline.shp") landc<-readShapeSpatial("landcover.shp") ro<-readShapeSpatial("roads.shp") bc<-gBuffer(ro,width=100) landc$ratings=1 landc$ratings[landc$LANDUSE_ID=...
  • 1,236
22 votes
2 answers
15k views

Using R to calculate the area of multiple polygons on a map that intersect with another overlaid polygon

I have a shapefile downloaded from the Ordnance Survey that gives electoral ward (division) boundaries for a county of the United Kingdom. I have successfully used R to load the shapefile and plotted ...
  • 1,249
22 votes
3 answers
13k views

Performing Raster Noise Reduction and Edge Smoothing?

I have a raster image like this I want to delete the isolated noise point and smooth the edge (say the white single points, and I want the edges of the entity look smooth). How can I do this in ...
  • 2,205
22 votes
3 answers
5k views

Using R with ArcGIS Desktop?

I've used the integration of R with QGIS, and found it a very uesful tool. Having taken a look at the R toolbox extension for 9 and the extension for 10 it appears they both offer only point ...
  • 15.4k
21 votes
7 answers
11k views

How to stop writeOGR from abbreviating Field Names when using "ESRI Shapefile" driver

I'm currently using the following script to add some attribute data from a table to lots of individual shapefiles: library(rgdal) specieslist <- read.csv("SpeciesList1.txt", header=F) attdata <-...
  • 1,658
20 votes
3 answers
9k views

Reverse clipping (erasing ) in R?

A reverse clip saves only the part of your spatial object that is outside the bounds of another object, as opposed to a regular clip which saves the parts that are inside the other object. ...
20 votes
3 answers
56k views

Checking if points fall within polygon Shapefile

Zillow has a set of shapefiles for different neighborhoods of major US cities. I wanted to check if certain buildings were present in certain neighborhoods using R: library(rgeos) library(sp) library(...
  • 303
20 votes
1 answer
21k views

Create polygons of the boundary of a Raster in R

I want to create two polygons. One is of the rectangular extents of my raster. I know I can obtain the extent of the raster using r <- raster("band5.tif") e <- extent(r) plot(e) gives me this: ...
  • 624
19 votes
5 answers
42k views

Converting osm file to shapefile (or data frame) in R

I was looking for a road shapefile for Australia today. I ended up going to Geofabrik. Open Street Map has data in .osm format. But they do not have one in .shp for Australia. If possible, I want to ...
  • 505
19 votes
2 answers
30k views

How to make RASTER from irregular point data without interpolation

I was trying to make a raster image from an irregularly spaced point database. The data looks like- > head(s100_ras) x y z 1 267573.9 2633781 213.29545 2 262224.4 2633781 ...
  • 543
19 votes
2 answers
11k views

Fixing orphaned holes in R

I'm trying to perform a union on a common field after merging two adjacent shapefiles. The shapefiles end up with at least one thin sliver of space between them. When I attempt a union I get the ...

1
2 3 4 5
106