Closed rosemckeon closed 5 years ago
I just dug out some of the 10+ year old code from the first or second year of my PhD. You might want to check on how accurate the zone is, and on the library PBSmapping.
The data
argument should be a data matrix or data frame. The Lat
and Lon
should be the columns for the latitude and longitude coordinates in data
, respectively.
#Converts Lat-Lon into UTM.
library(PBSmapping);
LLtoUTM <- function(data,Lat,Lon,km=TRUE){
dat <- cbind(data[,Lon],data[,Lat]);
Coo <- as.data.frame(x=dat,rownames=dim(data)[1]);
zns <- seq(from=-180,to=180,by=6);
Nor <- seq(from=0,to=80,by=8);
let <- c("N","P","Q","R","S","T","U","V","W","X");
mat <- NULL;
unq <- NULL;
nor <- NULL;
for(i in 1:dim(dat)[1]){
zne <- sum(dat[i,1] > zns);
nor[i] <- let[sum(dat[i,2] > Nor)];
mat <- rbind(mat,c(zne,dat[i,]));
if(sum(unq==zne) == 0){
unq <- c(unq,zne);
}
}
ord <- order(x=mat[,1]);
mat <- mat[ord,];
br1 <- 0;
nwz <- NULL;
for(i in 1:length(unq)){
br2 <- br1 + sum(unq[i]==mat[,1]);
nwz[[i]] <- as.matrix(t(mat[(br1+1):br2,]));
br1 <- br2;
}
br1 <- 0;
nwz <- NULL;
for(i in 1:length(unq)){
br2 <- br1 + sum(unq[i]==mat[,1]);
nmt <- matrix(data=mat[(br1+1):br2,],ncol=3);
nwz[[i]] <- as.data.frame(nmt);
colnames(x=nwz[[i]]) <- c("zone","X","Y");
br1 <- br2;
}
utm <- NULL;
for(i in 1:length(nwz)){
attr(x=nwz[[i]],which="zone") <- nwz[[i]][1,1];
attr(x=nwz[[i]],which="projection") <- "LL";
utm <- rbind(utm,convUL(xydata=nwz[[i]],km=km));
}
utm <- utm[order(ord),];
utm <- cbind(utm[,1],nor,utm[,2:3]);
colnames(x=utm) <- c("Zone E","Zone N","Easting","Northing");
rownames(x=utm) <- 1:dim(utm)[1];
return(utm);
}
Let me know if it doesn't work (or if it does!).
Thanks @bradduthie! I'll give it a whirl.
Tried Edinburgh, Belfast, Chicago, and Seoul. It seems to work. You just need to use decimal, and anything West or South should be negative (i.e., lat-lon coordinates of Belfast are Lat: 55.9533, Lon: -3.1883).
Looks like this works great @bradduthie! Not tried plotting yet but the conversion runs nicely.
From: Brad Duthie notifications@github.com Sent: 18 September 2019 09:53 To: StirlingCodingClub/studyGroup studyGroup@noreply.github.com Cc: Rose McKeon crm00032@students.stir.ac.uk; Author author@noreply.github.com Subject: Re: [StirlingCodingClub/studyGroup] Has anybody got any advice for working with lat/long data and converting units to/from metres? (#31)
Tried Edinburgh, Belfast, Chicago, and Seoul. It seems to work. You just need to use decimal, and anything West or South should be negative (i.e., lat-lon coordinates of Belfast are Lat: 55.9533, Lon: -3.1883).
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/StirlingCodingClub/studyGroup/issues/31?email_source=notifications&email_token=ABEZ2NGG6ATPP62UTI626GDQKHUCNA5CNFSM4IXRBUH2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD67KUVI#issuecomment-532589141, or mute the threadhttps://github.com/notifications/unsubscribe-auth/ABEZ2NDRUXJLCBAC5ZVGMRLQKHUCNANCNFSM4IXRBUHQ.
The University achieved an overall 5 stars in the QS World University Rankings 2018 The University of Stirling is a charity registered in Scotland, number SC 011159.
Glad it works @rosemckeon -- looking at it now, you might need to tweak it again if you're doing anything in the Southern Hemisphere. Otherwise, I think it should be fine.
@bradduthie All my coordinates are for Portugal, but I'll bear that in mind for the future!
From: Brad Duthie notifications@github.com Sent: 18 September 2019 11:30 To: StirlingCodingClub/studyGroup studyGroup@noreply.github.com Cc: Rose McKeon crm00032@students.stir.ac.uk; Mention mention@noreply.github.com Subject: Re: [StirlingCodingClub/studyGroup] Has anybody got any advice for working with lat/long data and converting units to/from metres? (#31)
Glad it works @rosemckeonhttps://github.com/rosemckeon -- looking at it now, you might need to tweak it again if you're doing anything in the Southern Hemisphere. Otherwise, I think it should be fine.
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/StirlingCodingClub/studyGroup/issues/31?email_source=notifications&email_token=ABEZ2NGAIHMWZQKNZLND373QKH7MTA5CNFSM4IXRBUH2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD67TMDY#issuecomment-532624911, or mute the threadhttps://github.com/notifications/unsubscribe-auth/ABEZ2NGMR2YW3TYKWTRXSOTQKH7MTANCNFSM4IXRBUHQ.
The University achieved an overall 5 stars in the QS World University Rankings 2018 The University of Stirling is a charity registered in Scotland, number SC 011159.
(email reply is weird)
I have a bunch of data with lat/long values, as well as location accuracy (m). I'm trying to plot
lat ~ long
for each data point, with a larger circle around each point showing the location accuracy. So, in effect creating a basic map. But, I'm getting confused with the scales because lat and long are not metric (1 lat = ~111 Km nr the equator). So, ideally, I think I need to convert my location accuracy to the same units as lat/long. Then I could set point size to be defined by that column. Can anybody help?Here's a snippet to create similar data: