-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathAdd Polygons to Points
103 lines (84 loc) · 3.34 KB
/
Add Polygons to Points
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#Required Libraries. Uncomment to install only the first time you use.
#install.packages("sp")
#install.packages("maptools")
#install.packages("rgeos")
#install.packages("rgdal")
#Install Required Packages
library(sp)
library(maptools)
library(rgeos)
library(rgdal)
library(plyr)
#Defines location of input files
directory = "/Users/msolbrig/Documents/Bairros_Algar_Concessao_region"
polygon_file = "Bairros_Algar_Concessao_region"
point_file = "points.csv"
output_name = "outfile.csv"
combine_points_poly = FALSE
#Function for importing shapefiles to R and dealing with projection
ImportToR <- function(dsn, layer){
setwd(dsn)
ShapeFile <- readOGR(dsn = dsn, layer = layer)
if(!is.na(is.projected(ShapeFile)) && is.projected(ShapeFile)){
standard <- CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +towgs84=0,0,0")
ShapeFile<- spTransform(ShapeFile, standard)
}
return(ShapeFile)
}
#Gets and formats data
setwd(directory)
polygons <- ImportToR(directory, polygon_file)
points <- read.csv(file=point_file)
#separate values with nulls in lat/lon from rest of data
points_null_lat = points[is.na(points['Lat']),]
points_has_lat = points[!is.na(points['Lat']),]
#turns into spatial data type
points_coords = subset(points_has_lat, select = c(Lon, Lat))
points_data = subset(points_has_lat, select=-c(Lon, Lat))
pts = SpatialPointsDataFrame(points_coords, points_data)
proj4string(pts) <- proj4string(polygons) #Ensures that the projections match
#Adds the polygon to the points
pts_with_polygon <- cbind(as.data.frame(pts), over(pts, polygons))
#Adds the rows with Null lat/lon back in
output = rbind.fill(pts_with_polygon, points_null_lat)
#Write to CSV
if(combine_points_poly):
# function for reshaping the polygons for Tableau
ReshapePolygons <- function(ShapeFile){}
Data <- as(ShapeFile, "data.frame")
Data$ObjectID <- as.numeric(rownames(Data))
#extracts the coodinates and polygon IDs
ObjectList <- slot(ShapeFile,"polygons")
coordinates <- list(Latitude = numeric(0),
Longitude = numeric(0),
PolygonID = numeric(0),
ObjectID = numeric(0),
PlotOrder = numeric(0))
#Loops over Objects to extract coordinates
PolygonID <- 0
for(i in 1:length(ObjectList)){
Object <- ObjectList[[i]]
ObjectID <- slot(Object, "ID")
#Loops over Polygons in Object to extract coordinates
for(j in 1:length(slot(Object,"Polygons"))){
Polygon <- slot(Object,"Polygons")[[j]]
#excludes holes. This could be made an option.
if(!slot(Polygon,"hole")){
coords <- data.frame(slot(Polygon,"coords"))
coords$PlotOrder <- c(1:nrow(coords))
coordinates$PlotOrder <- c(coordinates$PlotOrder, c(1:nrow(coords)))
coordinates$Longitude <- c(coordinates$Longitude, coords[,1])
coordinates$Latitude <- c(coordinates$Latitude, coords[,2])
coordinates$ObjectID <- c(coordinates$ObjectID, rep(ObjectID,nrow(coords)))
coordinates$PolygonID <-c(coordinates$PolygonID, rep(PolygonID,nrow(coords)))
PolygonID <- PolygonID + 1
}
}
}
coordinates <- data.frame(coordinates)
CombinedData <- merge(Data,coordinates)
return(CombinedData)
}
polydata <- ReshapePolygons(polygons)
ouput <-
write.csv(output, file = output_name)