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

Thomas Petzoldt thpe at hhbio.wasser.tu-dresden.de
Thu Sep 8 12:32:41 CEST 2005

```Werner Wernersen wrote:
> Hi!
>
> I have no idea if this is maybe an easy task utilizing
> R since I read there is
> geographical map data in some package:
>
> I have a huge number of geographical points with their
> coordinates in Germany.
> Now I want to determine for each point in which
> "Bundesland" = state it is located.
>
> Can anybody tell me if this is doable relatively easy
> in R and if so give me
> some hints or links how to do it?
>
> Thanks a million,
>    Werner

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:
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)

plot(ger)

pger <- Map2poly(ger)
sx<- pger[]
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=".")
##########################################################

```