How to extract polygons from raster? - vector

The goal is to get the vector polygons from raster areas accordingly to their gray scale. For example:
Dark pixels to dark_relief.shp
Gray pixels to gray_relief.shp
Light pixels to light_relief.shp
First I got GeoTIFF from SRTM file using Raster – Analysis – DEM (Terrain models) in Hillshade mode:
Then I use Raster - Conversion - Polygonize (Raster to Vector), but result is too many small polygons. Obviously I should prepare the image before a conversion.
Any hints what to do?
May be other right way exists?
Regards!

If you know the cut off points for each contour/band/classification you can reclassify your raster file in qGIS with the raster calculator. From there you can use raster to vector to create polygons only for the contours/bands/classifications you specified.
The example raster calculator calculation below should divide a layer into three pieces, areas below 1000, areas between 1000 and 1200, and areas above 1200.
("layer" > 1000) * 1 + ("layer" > 1200) * 1
With a starting raster of:
And the above calculation, I got:
Which will polygonize pretty easily and cleanly.

Related

Extracting coordinates of 50m squares from shapefile in R

I'm trying to extract the coordinates of 50m squares and the flooding factor associated with that square, from data provided by the Environment agency, here (https://data.gov.uk/dataset/risk-of-flooding-from-rivers-and-sea1). I've downloaded the shapefile format, when you click through from that page to this one (http://environment.data.gov.uk/ds/catalogue/#/8d57464f-d465-11e4-8790-f0def148f590).
The data claims to show the flooding factor for each 50m square. I'm completely new to Shapefiles and also new to R. From what I've read I expect the 50m squares to be represented by 'Polygons' and have viewed the Polygons using
polys <- slot(data,"polygons")
and then printing the coordinates of a few of them using
for (i in 1:length(polys)) {
print(paste("Polygon #",i))
print(slot(slot(polys[[i]],"Polygons")[[1]],"coords" ))
}
I'm confused by the output as I assumed a square would be specified by four points, however, the number of (pairs of) coordinates specifying the polygons varies greatly.
Is this assumption correct? Or does the data not consist of 50m squares as it claims?
If they are indeed 50m squares, is there an easy way to extract the coordinates of the centre of the polygons and their IDs?

How to generate vector arrows that conforms to a raster slope layer?

I would like to generate vector arrows that conform to the topography/slope of a raster dataset of a river catchment area.
I have created a Fishnet grid of points in ArcGIS and I would like to create a single arrow for each point of a set length that will follow the shape of the slope i.e. follow the path of least resistance, the line will follow progressively small numbers in a 3 x 3 grid.
I think I can generate the vector arrows using vector plot. Is it possible to achieve the lines conforming to the raster?
UPDATE: I have ~200,000 lines that I generated from a grid of points. I am going to turn these into a raster using R and set it to the same resolution as my slope raster.
Any ideas on how to layer the raster lines on the slope so I can get the lines to follow the lowest values of the slope?
This is for display/mapping purposes only? Use a DEM or TIN and display your arrow lines in ArcScene.
EDIT: given your update about your data and software not working-
Try this:
1) Make a raster surface covering the extent of your data with a cell size of 100m (or smaller or larger if that doesn't suit)
2) Convert that raster to a polygon layer e.g. 'area_grid100m'
3) Do a spatial join and assign all points a polygon cell id from one of the unique id fields in 'area_grid100m'
4) Use Summarize to get the mean lat/long of the start points and mean lat/long of the end points for each polygon. Summarize on the polygon id field and get select mean for both the lat and long fields
5) Add summary table to ArcMap, right click and select Display XY Data (set X Field as longitude and y Field as latitude). Right right the result and select Data > Export Data to make it permanent. You will now have two points per 'area_grid100m' cell.
5) Recreate your lines using this new file, which will give you one line per cell
If the res is not small enough, make the 'area_grid' cells smaller.

How do I scale worldHires to plot coastlines correctly on R?

I'm trying to overlay the coastlines and country borders onto the R map I've drawn.
I'm trying the simple worldHires function to do this but it's not scaling correctly to my map. I downloaded my data from https://www.esrl.noaa.gov/psd/data/gridded/data.UDel_AirT_Precip.html and the file name is precip.mon.total.v401
This is my code when I try to plot my map (excluding the first few lines of reading data and variables into R):
>image.plot(lon,lat,precip[,ncol(precip):1,8],
main="Precipitation (August, 2001)",
xlab=expression(paste("Longitude(",degree,"E)")),
ylab=expression(paste("Latitude(",degree,"N)")),
zlim=c(0,15),xlim = c(min(lon),max(lon)),
ylim = c(min(lat),max(lat)),cex=0.5)
> map('world2Hires', fill=FALSE, add=T)
The boundary layer from world2Hires is very small, scaling from -90 to 90 for Latitude and 0 to 360 for Longitude.
My data's range is Latitude -180 to 180 and Longitude 0 to 720. So how do I scale the world2Hires to match my data? Or is that not possible? See image in link
In your question, you seem to be mixing the latitude values (-90:90) degrees with the matrix indices. Since the map is of resolution 0.5 degrees, these run 0:360. The same for the longitudes.
So from your question, I am not sure whether the problem is that your vectors "lon" and "lat" are incorrect (they should probably be something like lon=seq(0,360,by=0.5); lat=seq(-90,90,by=0.5)). Obviously, the map() command expects the co-ordinates to be in degrees.
But the illustration on the link you provided, shows that the data is on longitude [0,360] while the data in worldHires is on [-180,180].
I think the simplest solution is to use "world2Hires", which is the same map but with longitudes shifted to [0,360], which should fit your map.
As a side remark, using worldHires is possibibly not the best choice of world map. The country borders in some parts of the world are outdated, and the high resolution is not so important at a global scale. Just using "world2" (the "2" again is for the shift to [0,360]) would probably look just as good on a global map.
I may add that in the very near future, the next version of "map" will make such shifting of longitudes much easier, whithout the need for an extra data set.

Png image crop, and plot

Is there a simple way to crop a specified pixel location of a png image, and raster that image onto a plot.
Currently i am working with dissociated cells (up to 1000 cells in a field of view). Each cell has a center x (in pixels) and a center y (in pixels). We gather data in trace format, so i would like a way of displaying the picture of the cells next to the trace.
I have tried rasterImage, and grid.raster. Unfortunately i have no idea how to call upon the image array to specify the location.
Also, i do not want to use the zoom package since the functions work extremely slow and are not able to raster onto a current plot.
Take a look at the raster package. The raster::raster function, for importing or coercing your png and raster::crop for subsetting to a specific extent.
Here is an example from the crop functions help.
Create an example raster
r <- raster(nrow=45, ncol=90)
r[] <- 1:ncell(r)
Subset the raster based on a define extent
e <- extent(-160, 10, 30, 60)
rc <- crop(r, e)
Plot results
par(mfrow=c(1,2))
plot(r)
plot(rc)
It was alot easier than expected. My PNG image was an array with 3 layers.
img[xpos, ypos, layer]
Therefore i could simply specify the location of my cells increased by a factor,
rasterImage(img[(x-x*.2):(x+x*.2), (y-y*.2):(y+y*.2),],-2, 4, 3, 1)
Now i need to code around for cells at the limits of my plots.

Dividing polygon value according to number of raster cells overlayed in R

At the moment I'm working with the raster package. I've different polygons with certain values (let's say 100), which I managed to rasterize. The problem is that when I rasterize each raster cell result with a value of 100, but I want the polygon value (100), to be equally divided per each cell overlaying the polygon. For example, if the polygon overlay 100 raster cells, I want each raster cell to have a value of 1, instead of 100. Could anyone help me?
Here the raster abd the shp I created: https://drive.google.com/drive/folders/0B6-UFgI67v99c3ZhUFp0eWpzOGM
I tried to do something like that:
ncell<-freq(union,digits=6)
ncell[,"value"]/ncell[,"count"]
new<-rep(c(union[,"value"],ncell[,"count"]))
union$new<-c(new)
but I cannot join the column I obtain in the raster associating the raster cells with the new values.
There are two ways I can think of:
compute the number you want for the polygons, before using rasterize
use freq as you did, but then use subs
for example:
r <- subs(union, data.frame(ncell))
x <- union / r

Resources