[R-sig-Geo] converting latitude and longitude to UTM

Abby Rudolph arudolph at PIRE.org
Thu Jan 31 14:03:36 CET 2013


Hello,

I am using the following code to create ppp files from csv data and map shape files, but I am getting some errors which I have been unable to fix by searching them online:

library(spatstat)
library(maps)
library(maptools)

NYC2<-readShapePoly("nybb.shp") # this is a map of the NYC boroughs without waterways and no census tract divisions (but it does include lines separating the 5 boroughs)
plot(NYC2)

NYBorough<-readShapePoly("NYBoroughsShapeMerge.shp") # this is a map of the NYC boroughs with census tract divisions
plot(NYBorough)

myfile<-read.csv("myfile.csv")
# my csv file contains 4 columns (ID, variable_type, X, Y)
# my goal is to determine whether individuals of variable_type=1 are spatially distributed more than what would be expected due to chance, if individuals of variable_type=0 are spatially distributed more than what would be expected due to chance, and finally if the spatial distribution of variable_type=1 differs from that of variable_type=0.

x<-NYBorough at polygons[[1]]@Polygons
# ppp(x.coordinates, y.coordinates, x.range, y.range)
coords<-slot(x[[1]],"coords")
coords
coords<-coords[19:1,]
coords<-coords[-1,]
coords

u<-myfile$type==0

Type_0<-ppp(myfile$X[u],myfile$Y[u],
                window=owin(poly=list(x=coords[,1],y=coords[,2])))
Type_1<-ppp(myfile$X[!u],myfile$Y[!u],
                window=owin(poly=list(x=coords[,1],y=coords[,2])))

ERROR Message:
Warning message:
In ppp(myfile$X[u], myfile$Y[u], window = owin(poly = list(x = coords[,  :
  217 points were rejected as lying outside the specified window
Warning message:
In ppp(myfile$X[!u], myfile$Y[!u], window = owin(poly = list(x = coords[,  :
  435 points were rejected as lying outside the specified window

# I only have 652 points, so all of my points are rejected as lying outside the specified window


Below is the sample code I was using as a template:

x<-BaltCity2 at polygons[[1]]@Polygons
coords<-slot(x[[1]],"coords")
coords<-coords[167:1,]
coords<-coords[-1,]
coords.mi<-coords*0.000621371192
# convert meters to miles

play2<-ppp(play$X_m*0.000621371192,play$Y_m*0.000621371192,
           window=owin(poly=list(x=coords.mi[,1],y=coords.mi[,2])))

There are two things I can think of that might be different between my data set and the data that is used for this template code - and might cause the warning:
1) My shapefile is for NYC and includes 5 different boroughs, whereas the shapefile for Baltimore looks more like a regular polygon (like a rectangle).
2) My data has only lat/lon coordinates and the data corresponding with the template code I am working off of is using what I think is UTM (X_m and Y_m).

Is there another way I can run the code above that will utilize the latitude and longitude coordinates and will not return a warning?

Thanks,
Abby
________________________________________
From: Abby Rudolph
Sent: Wednesday, January 30, 2013 2:54 PM
To: r-sig-geo at r-project.org
Subject: converting latitude and longitude to UTM

Hello,
Is there a formula to convert x-/y-coordinates (latitude /longitude) into UTM.  My data is in the form of latitude and longitude, but the code I wish to use relies on x_m and y_m which I believe are UTM measurements (x in meters and y in meters).
Thanks
Abby



More information about the R-sig-Geo mailing list