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

MacQueen, Don macqueen1 at llnl.gov
Fri Feb 1 16:55:22 CET 2013


To get started, try the following:

require(sp)

bbox(NYC2)
bbox(NYBorough)
range(myfile$X)
range(myfile$Y)

and see if the ranges of X,Y are even close to the coordinate ranges of
your shapefiles.

Then try

coordinates(myfile) <- c('X','Y')
bbox(myfile)
plot(myfile)

If the problem is what you suspect, i.e., lat/long vs. UTM (which seems
likely), then you will have to start learning about how to specify
coordinate systems in R. It's a much bigger topic than I can go into here.
See the help for
  ?proj4string
  ?spTransform   (as suggested by Mike Sumner in another response)


You will need the rgdal package for spTransform.

For what it's worth, I use Quantum GIS (QGIS) when I need to find out
information about a particular projection.

-Don

-- 
Don MacQueen

Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062





On 1/31/13 5:03 AM, "Abby Rudolph" <arudolph at PIRE.org> wrote:

>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
>
>_______________________________________________
>R-sig-Geo mailing list
>R-sig-Geo at r-project.org
>https://stat.ethz.ch/mailman/listinfo/r-sig-geo



More information about the R-sig-Geo mailing list