Skip to content
Prev 76864 / 398502 Next

Using R map data to determine associated state for a coordinate?

Werner Wernersen wrote:
Hello Werner,

two building blocks, but don't know if the precision meets your needs.

1. Do you have a good map of Germany *with* Federal States?

* If YES and if it's free:
==> I would be interested! Please post it's source.

* If NO:
==> Downloadable map data are available on:
     http://www.vdstech.com/map_data.htm

2. The following approach reads and converts a shapefile with functions 
from maptools and then follows the example of inside.owin() from the 
spatstat package.

Hope that helps

Thomas Petzoldt


##########################################################
library(maptools)
library(spatstat)

ger <- read.shape("germany.shp")
plot(ger)

pger <- Map2poly(ger)
sx<- pger[[13]]
lines(sx, type="l", col="red") # Saxony ;-)

## Create an owin (observation window) object
# direction of coordinates must be reversed, in some cases
# if error message: remove rev()'s
saxony <- owin(poly=list(x=rev(sx[,1]), y=rev(sx[,2])))

# random points in rectangle
x <- runif(1000, min= 6, max=15)
y <- runif(1000, min=46, max=56)

ok <- inside.owin(x, y, saxony)

points(x[ok], y[ok])
points(x[!ok], y[!ok], pch=".")
##########################################################