Spacedman
  • Member for 11 years, 4 months
  • Last seen this week
How to calculate polygon centroids in R (for non-contiguous shapes)
Accepted answer
61 votes

Firstly, I can't find any documentation that says that coordinates or getSpPPolygonsLabptSlots returns the centre-of-mass centroid. In fact the latter function now shows up as 'Deprecated' and should ...

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

Several steps required: You say it's a regular 1km grid, but that means the lat-long aren't regular. First you need to transform it to a regular grid coordinate system so the X and Y values are ...

View answer
Reading KML file into R?
Accepted answer
40 votes

To read a KML with the OGR driver, you give it the file name and the layer name. Roger's comment is that the layer name is hidden in the KML file, and unless you know how the KML was created you can'...

View answer
Clipping raster in R
Accepted answer
39 votes

I would drop using the maps package and find a state shapefile. Then load that into R using rgdal, and then do some polygon overlay work. library(raster) # use state bounds from gadm website: # us = ...

View answer
Working with PostGIS data in R?
Accepted answer
34 votes

If you have PostGIS driver capability in the rgdal package then its just a question of creating a connection string and using that. Here I'm connecting to my local database gis using default ...

View answer
Seeking QGIS plugin to work with What3Words
25 votes

Using a simple w3w wrapper here: https://github.com/what3words/w3w-python-wrapper I wrote a custom python function to return the what3words given a location. Once hooked into the expression engine, ...

View answer
how to overlay a polygon over SpatialPointsDataFrame and preserving the SPDF data?
Accepted answer
23 votes

From the sp::over help: x = "SpatialPoints", y = "SpatialPolygons" returns a numeric vector of length equal to the number of points; the number is the index (number) of the polygon of ‘y’...

View answer
Buffering only sides of polygons in QGIS
Accepted answer
22 votes

Starting with a polygon (or several polygons): Run the "Polygons to Lines": Then "Explode lines". This won't look any different but if you enable "Show feature count" ...

View answer
How to combine sfc objects from R package sf
Accepted answer
17 votes

Just use c like its a vector: > (sfc12 = c(sfc1, sfc2)) Geometry set for 2 features geometry type: POINT dimension: XY bbox: xmin: 0 ymin: 1 xmax: 1 ymax: 1 epsg (SRID): NA ...

View answer
Converting latitude and longitude to point in geodjango?
Accepted answer
17 votes

Straight from the documentation: Another option is to use the constructor for the specific geometry type that you wish to create. For example, a Point object may be created by passing in the X ...

View answer
Reverse clipping (erasing ) in R?
16 votes

Seems to be a simple application of gDifference from the rgeos package: > require(rgeos) > ukhole = gDifference(uk, lnd) Warning message: In RGEOSBinTopoFunc(spgeom1, spgeom2, byid, id, "...

View answer
Create area buffers that are limited by a coastline
Accepted answer
15 votes

The area of a circular buffer is a monotonically-increasing function of buffer radius (on a planar coordinate system anyway). So a simple search strategy can find a radius R such that the area of the ...

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

Don't use readShapePoly - it ignores the projection specification. Use readOGR from the sp package. For geographic operations such as your polygon overlay, check out the rgeos package. Literally the ...

View answer
How to produce spatial grid from raster?
Accepted answer
14 votes

You can get all the non-NA coordinates of cells in a raster with: r = raster(matrix(runif(20),5,4)) r[r>.5]=NA coordinates(r)[!is.na(values(r)),] x y [1,] 0.375 0.7 [2,] 0.125 0.5 [...

View answer
Applying filter with the sf package not working
Accepted answer
14 votes

You've not attached the dplyr package: > e %>% filter(ADMIN_NAME=="Durham") Error in data.matrix(data) : (list) object cannot be coerced to type 'double' When you do, you'll see that dplyr ...

View answer
Create a shapefile from dataframe in R, keeping attribute table
Accepted answer
14 votes

You are overcomplicating things. > WGScoor<- MyData > coordinates(WGScoor)=~long+lat > proj4string(WGScoor)<- CRS("+proj=longlat +datum=WGS84") at this point you have something you ...

View answer
Get Raster Values from a Polygon Overlay in Opensource GIS Solutions
Accepted answer
14 votes

Use 'extract' to overlay polygon features from a SpatialPolygonsDataFrame (which can be read from a shapefile using maptools:readShapeSpatial) on a raster, then use 'table' to summarise. Example: >...

View answer
Google Maps not Showing in QGIS 2.12
13 votes

I suggest you drop the OpenLayers plugin for background maps and try the Quick Map Services plugin instead. It has more layers than OpenLayers, seems more robust, directly accesses map tiles rather ...

View answer
Calculate distance between points and nearest polygon in R
Accepted answer
13 votes

Let's set up some sample data: library(sp) library(spdep) example(columbus) plot(columbus) We'll now get some points. Click 20 times on the map, some inside and some outside polygons: pts = locator(...

View answer
Merge a list of spatial polygon objects in R
Accepted answer
13 votes

Given a list of SpatialPolygons objects, here's how to construct a spatial polygons data frame with one feature per original SpatialPolygons feature. Sample data: spl is a list of 12 SpatialPolygons ...

View answer
Load a CSV as Simple Features R
Accepted answer
12 votes

When you read a CSV with no options the driver doesn't know where the coordinate columns are so returns a data frame: library(sf) > st_read("./pts.csv") Reading layer `pts' from data source `/home/...

View answer
How do you split a circle polygon into equal quadrants in QGIS?
12 votes

5 degrees is 1/72 of a circle. Use the "Minimum enclosing circles" tool to create a circle with 72 segments. Use "Extract Vertices" to turn this circle into 72 points in a layer called Ring. ...

View answer
Bulk csv to shapefile (using ogr2ogr)
Accepted answer
12 votes

A modern-enough GDAL can convert CSV files directly into shapefiles. For the test.csv file in the documentation, http://www.gdal.org/drv_csv.html - you can do: ogr2ogr -s_srs EPSG:4326 -t_srs EPSG:...

View answer
Getting user's QGIS version using PyQGIS?
Accepted answer
12 votes

In QGis < 3, qgis.core has a QGis object: >>> from qgis.core import QGis with various formats of the version string: >>> QGis.QGIS_VERSION u'2.18.5' >>> QGis....

View answer
QGIS How to label individual sides of polygon
Accepted answer
12 votes

The "networks" plugin in QGIS has a facility to split polygons into individual line segments which you can then label with length. I discovered this by trying various search terms in the plugin ...

View answer
One dimensional map of the world?
12 votes

What you can do is cover your 2-d space with a 1-d space-filling curve, such as a Peano curve or Hilbert Curve. Then you map your points onto the nearest point on the curve. Unwrap the curve and you ...

View answer
Method for generating wedge buffer in PyQGIS
Accepted answer
11 votes

Yes. Import these: >>> from qgis.core import QgsGeometry, QgsPoint Then: >>> QgsGeometry.createWedgeBuffer(QgsPoint(1,2), 25, 5, 2, ) <QgsGeometry: CurvePolygon (CompoundCurve (...

View answer
sf geometry column naming differences R
Accepted answer
11 votes

I've just written this which I think will safely rename the geometry column by changing the names element and then setting the geometry column with st_geometry - which will affect the sf_column ...

View answer
Counting the occurrence of a specific letter within a string (QGIS)?
11 votes

Here's a way to find the number of Gs. Replace everything that isn't a G with an empty string, and count the length of what is left: length(regexp_replace('Urban,Green,Urban,Green,Green','[^G]','')) -...

View answer
To project, or not to project? Extracting raster values with R
Accepted answer
11 votes

Reprojecting rasters is usually a bad thing to do. It involves a non-reversible transformation from one grid system to another grid system that can have a non-linear relationship to the first. Hence ...

View answer
1
2 3 4 5
42