Convert an image file into a shape file - r

I have a high resolution layout of a floor (in a building) which I would like convert to a shapefile or polygons. The image is a JPEG, it outlines the walls of the rooms and the locations of beds in the on the floor. I would like to convert that JPEG image into a shapefile, or polygon, so that I can overlay a predictive model onto it.
I have done this with simpler shapes. For example, using the geom_polygon tutorial (http://docs.ggplot2.org/0.9.3.1/geom_polygon.html) so I know it can be done. But the layout is more complex and if I can I would like to overlay posterior probabilities of my predictive model overtop of the true ward layout. Right now, this involves me mapping out the co-ordinates of the wall intersections and create polygons that way (doable, but it takes a long time).
It would be ideal to convert the room layout into a shape file, and load that into R instead (once I have the shape file, I've converted it to a polygon to use in ggplot, so that isn't the issue).
Here is an example of what my layout data looks like
The thicker lines represent walls and the small rectangles are beds. What I want is to input this into R and that the points line up to a cartesian co-ordinate system, it would also be good to get the bed co-ordinates as well. I don't have access to ARCGis..or any geographers...

Related

Mapping how many points are within a radius of every location in R

In R, I am trying to create a choropleth map. I have built a database of businesses, some are part of chains (e.g. McDonalds) and others are independent. I want to calculate how many businesses are within 30km of each point on the map, but treat the different locations of chains as a single business.
For example, if you a point is:
5km from a McDonalds,
10km from Taco Bell
15km from Chick-Fil-A
20km from KFC
25km from McDonalds
35km from Five Guys
The colour will show that there are 4 fast food outlets within 30km.
I am happy to use any R package but I am mostly familiar with tmaps and ggplot2 maps.
At this stage the best approach I can think of is to create polygons for each chain and stack them as transparent layers of the same colour. I don't think this would be very efficient and wouldn't create a very nice looking choropleth.
The other answers I could find were either counting points (e.g https://gis.stackexchange.com/questions/229066/counting-how-many-times-a-point-is-inside-a-set-of-intersecting-polygons-in-r) or for GIS software.
EDIT:
I have managed to create a 30km radius from every location of every chain (using rgeos gIntersection). I now have a series of polygons.
To solve my question the additional thing I need to do is create polygons for where:
Only one polygon covers the area,
Two polygons covers the area,
etc.
To try to visual is this I used the answer from https://gis.stackexchange.com/questions/229066/counting-how-many-times-a-point-is-inside-a-set-of-intersecting-polygons-in-r
In the linked question they are trying to count how many polygons cover the numbered points (the image on the right). What I am trying to do is to create the image on the left, where there are polygons of no overlap (1), two overlapping polygons (2) and so on.
I think what you are trying to accomplish would be best approached using a raster approach rather than a chloropleth. To make a chorlopleth, you define a set of (generally irregular) polygons, summarize something within each polygon, then color the polygons based on the attributes. This would be a good approach if you wanted to say how many fast food resteraunts are within each state or county, or how many fast food joints per capita by state.
From your description, however, you are looking for how many fast food joints within a set radius for all points. This is more of a raster question, since you can represent your data on a regular grid.
The raster package is a good start for working with raster data and works well with the sf package.
You need to determine what density you need to accomplish your goal, then use this to determine the resolution of your raster. Once you've got that you can use raster::rasterize() to summarize your (I'm assuming) point data.
I'm assuming you have an object that has the locations of each restaurant, I'll call this object "points".
library(raster)
library(sf)
# create raster template with 30km resolution (I'm assuming your projection is in meters)
raster_template = raster((extent(points),
resolution = 30000,
crs = st_crs(points)
)
# rasterize your point data
r = rasterize(points, raster_template, fun = "count")
This should create a grid where each cell has the number of points within each 30km cell. You should then be able to plot the raster, but may want to either clip or mask it to just show parts that are within New Zealand

Cel shading/alpha shape in current visualization

I am playing around with rgl and I have created a 3D rendering of the mouse brain, in which structures can be isolated and coloured separately.
The original data is a 3D array containing evenly spaced voxels.
Every voxel is coded with a structure ID.
Every structure is rendered separately as a mesh by marching cubes, and smoothed using Laplacian smoothing as implemented by Rvcg.
Some of these structures can be quite small, and it would make sense to look at them within the context of the whole brain structure.
One of the options is to create a low-threshold mesh of the whole set of voxels, so that only the outer surface of the brain is included in the mesh.
This surface can be smoothed and represented using a low alpha in rgl::shade3d colouring faces. This however seems to be quite taxing for the viewport as it slows down rotation etc especially when alpha levels are quite low.
I was wondering if there is any way to implement some sort of cel shading in rgl, e.g. outlining in solid colours the alpha hull of the 2D projection to the viewport in real time.
In case my description was not clear, here's a photoshopped example of what I'd need.
Ideally I would not render the gray transparent shell, only the outline.
Cel shading example
Does anybody know how to do that without getting deep into OpenGL?
Rendering transparent surfaces is slow because OpenGL requires the triangles making them up to be sorted from back to front. The sort order changes as you rotate, so you'll be doing a lot of sorting.
I can't think of any fast way to render the outline you want. One thing that might work given that you are starting from evenly spaced voxels is to render the outside surface using front="points", back="points", size = 1. Doing this with the ?surface3d example gives this fake transparency:
If that's not transparent enough, you might be able to improve it by getting rid of lighting (lit = FALSE), plotting in a colour close to the background (color = "gray90"), or some other thing like that. Doing both of those gives this:
You may also be able to cull your data so the surface has fewer vertices.

Using point coordinates and diameter to calculate areal projection of points

I have data on a number of ecological variables associated with spatial points. Each point has x & y coordinates relative to the bounding box, however the points represent circular areas of varying diameter. What I'm trying to achieve is to project the area occupied by each point onto the observation window so that we can subsequently pixellate the area and retrieve the extent of overlap of the area of each point with each pixel (grid cell). In the past I have been able to achieve this with transect data by converting to a psp line object and then using the pixellate function in the spatstat package but am unsure how to proceed with these circular areas. It feels like I should be using polygon classes but again I am unsure how to define them. Any suggestion would be greatly appreciated.
In the spatstat package, the function discs will take locations (x,y) and radii r (or diameters, areas etc) and generate either polygonal or pixel-mask representations of the circles, and return them either as separate objects or as a single combined object.

Curved vector graphics using paths

When drawing curved shapes in R, they are typically made up of a collection of short straight line segments (with circular line endings). The result looks good to the human eye but is a bit of a roundabout way of doing it as you need to interpolate the curve to get the coordinates of the segments. It also produces unnecessarily large files, although this is not a big problem unless the plot contains an extreme amount of curves.
Is there a way to create curved vector shapes the proper way using paths made up of anchor and pivot points as in the figure below?
Short answer: "No".
Long answer: R doesn't know if your output device supports describing curves from formulae - for example PDF and PostScript can (I think) have arcs and curves, but R doesn't know if its drawing to one of those or to a bitmap device.
Hence when you want to draw an arc on an R plot, it can't do:
0 0 moveto
25 25 pi arcto
(or whatever the PostScript is) to describe an arc that will be infinitely smoothly zoomable. It has to render the arc into segments.
If you run the example in help(bezierGrob) to a PDF device and zoom in, you can see the segments (zoomed in to 500%).

Vector GIS Data line-of-sight?

I have a situation where I'm only concerned with a few vector layers and two-dimensional line-of-sight. I know that line-of-sight is usually performed on raster data because the typical use is topography. Because that wording is vague and close to useless here's my situation:
I have a polygon shoreline vector shapefile, a "source" point placed in the water somewhere, and a "buffer" polygon layer that represents a large radius circle around the "source" point. I'm only interested in the parts of the buffer polygon that are "within sight" of the source point. In the image below the red dot is the source, the orange polygon is the buffer clipped with the shoreline, and the yellow polygon is what I'm interested in. Even this isn't as fine as I'd like.
Image: http://i.stack.imgur.com/IKBLv.png
I want to automate the process I use now (fairly time-consuming) and would prefer to use python/numpy/scipy/OGR/GRASS instead of ESRI's stuff.
Any idea how to trace along the line and check for "visibility"? I could rasterize everything and use a traditional radial line-of-sight script within GRASS but that seems like way dealing with too much data held in memory and running checks for pixels we know wouldn't produce a collision for the intersection of a few vectors. I want to be as light as possible while maintaining the highest accuracy possible.
How about considering (iteratively) the line between your point and each point in the shoreline shapefile? If it intersects the "land" polygon (crosses over land), then that point on shore is not visible. Take all the points that are visible, and use them to form a new polygon of the visible area.

Resources