Last active
November 1, 2016 22:40
-
-
Save dKvale/29801e2df6dc646cc7c9d8b2a20aa92c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
title: "Coordinate conversion in R" | |
output: html_document | |
--- | |
## R Script for converting coordinates (e.g., UTM to Lat/Long). | |
### Required packages | |
```{r } | |
library(sp) | |
library(maptools) | |
``` | |
### Load data table | |
```{r load} | |
# Receptors in UTM | |
data <- read.csv("M:\\MNRiskS 2011 development\\Receptors\\Backup receptors\\LAKES receptors used in MNRISKS 2011.csv", stringsAsFactors = F)[ , -c(4:5)] | |
# Show first rows | |
head(data) | |
``` | |
| Receptor| GEOID| FIPS_Code| utm_x| utm_y| zoneID|County | | |
|--------:|------------:|---------:|--------:|-------:|------:|:------| | |
| 46318| 270017701001| 1| 490997.4| 5174001| 27001|AITKIN | | |
| 46383| 270017701001| 1| 494000.8| 5173997| 27001|AITKIN | | |
| 46514| 270017701001| 1| 491001.9| 5177001| 27001|AITKIN | | |
| 46526| 270017701001| 1| 475999.8| 5176998| 27001|AITKIN | | |
| 46579| 270017701001| 1| 467001.5| 5176996| 27001|AITKIN | | |
| 46591| 270017701001| 1| 493996.2| 5176997| 27001|AITKIN | | |
### Convert to shapefile | |
```{r shapefile} | |
coordinates(data) <- ~utm_x + utm_y | |
``` | |
### Assign current projection | |
```{r projection} | |
# NAD83 / UTM zone 15N | |
proj4string(data) <- CRS("+init=epsg:26915") | |
# Alternative projections: | |
# WGS84 Web Mercator (Auxiliary Sphere) -> CRS("+init=epsg:3857") | |
# WGS 84 -> CRS("+init=epsg:4326") | |
``` | |
### Plot shapefile | |
```{r plot} | |
plot(data) | |
``` | |
### Transform projection | |
```{r transform coordinates} | |
data_new <- spTransform(data , CRS("+init=epsg:4326")) | |
``` | |
### Plot new data | |
```{r plot_new} | |
plot(data_new) | |
``` | |
### Convert shapefile back to data table | |
```{r back to data frame} | |
data_new <- data.frame(data_new, stringsAsFactors = F) | |
# Show first rows | |
head(data_new) | |
# Re-name coordinate columns to lat/long | |
names(data_new)[7:6] <- c("Lat", "Long") | |
``` | |
### Save new coordinates as .CSV | |
```{r save} | |
write.csv(data_new, "Transformed receptors.csv", row.names=F) | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment