-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGeneral_GISFunctions.R
122 lines (83 loc) · 4.44 KB
/
General_GISFunctions.R
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
rm(list=ls(all=TRUE)) # clear memory
packages<- c("ggmap","sp","taRifx.geo", "SpatialTools","plyr","rgdal") # list the packages that you'll need
lapply(packages, require, character.only=T) # load the packages, if they don't load you might need to install them first
#setwd("/mnt/smb/Research/OTool_Distances")
setwd("N:\\Research\\OTool_Distances")
#gmaps.key <- "AIzaSyDZy7HLVrouFTsULZ6D6ZyGub8iseJI_OU" #Unneeded!
IntersectPtWithPoly <- function(x, y) {
# Extracts values from a SpatialPolygonDataFrame with SpatialPointsDataFrame, and appends table (similar to
# ArcGIS intersect)
# Args:
# x: SpatialPoints*Frame
# y: SpatialPolygonsDataFrame
# Returns:
# SpatialPointsDataFrame with appended table of polygon attributes
# Set up overlay with new column of join IDs in x
z <- over(x, y)
# Bind captured data to points dataframe
x2 <- cbind(x, z)
# Make it back into a SpatialPointsDataFrame
# Account for different coordinate variable names
if(("coords.x1" %in% colnames(x2)) & ("coords.x2" %in% colnames(x2))) {
coordinates(x2) <- ~coords.x1 + coords.x2
} else if(("x" %in% colnames(x2)) & ("x" %in% colnames(x2))) {
coordinates(x2) <- ~x + y
} #end elseif
# # Reassign its projection if it has one
# if(is.na(CRSargs(x@proj4string)) == "FALSE") {
# x2@proj4string <- x@proj4string
# } #end if
return(x2)
} #End IntersectPtWithPoly
CSv.to.Shapefile <- function(df,x.coord, y.coord,projection, out.name = NULL, save = FALSE){
coordinates(df)=df[c(x.coord,y.coord)]
proj4string(df)=CRS(projection) # set it to lat/long
if (save){
writeOGR(df, dsn="." ,layer=out.name,driver="ESRI Shapefile")
}
return (df)
}#end CSV.to.Shapefile
all.respondants <- read.csv("all_participants_xy.csv")
unique.respondants <- unique(all.respondants[c(2,3)])
unique.respondants <- unique.respondants[complete.cases(unique.respondants),] #removing case #98797 which is NA for both coordinates
remove(all.respondants)
#unique.respondants <-unique.respondants[1:50,] #SAMPLE IT DOWN TO 50 FOR TESTINF
blocks.spdf <- readOGR(dsn = "GISData", layer = "Chicago_Blocks_Project") # you load a shapefile with the directory in dsn and the name of the file minus .shp as the layer
proj <- proj4string(blocks.spdf)
respon.spdf <- SpatialPoints(coords = unique.respondants, proj4string=CRS(proj))
test<- IntersectPtWithPoly(respon.spdf,blocks.spdf)
#gives the projection information
#A leaflet builder
rm(list=ls(all=TRUE)) # clear memory
packages<- c("maptools","rgdal","leaflet","raster") # list the packages that you'll need
lapply(packages, require, character.only=T) # load the packages, if they don't load you might need to install them first
setwd("E:\\GISWork_2\\Ferri_ImaginaryPlaces2\\Map\\")
latlong <- "+init=epsg:4326"
google <- "+init=epsg:3857"
in.csv <- read.csv("2015_StudentPoints.csv")
coordinates(in.csv) = ~Lon + Lat
proj4string(in.csv) = CRS(latlong)
writeOGR(obj = in.csv, dsn= "student", layer = "student", driver="GeoJSON")
writeOGR(obj = in.csv, dsn= "Old", layer = "student_points", driver="ESRI Shapefile")
shapes = readOGR(".", "data")
qqq<- readOGR(dsn= ".", layer = "student_points")
writeOGR(obj = qqq, dsn= ".", layer = "VasiDay1_points", driver="GeoJSON", verbose=TRUE)
plot(base.file)
qq <- leaflet() %>%
#setView(lng = 12.5, lat = 41.9, zoom = 12) %>%
# addRasterImage(image) %>%
addTiles(group = "OpenStreetMap") %>% # Add default OpenStreetMap map tiles
addMarkers(data = in.csv,
lat = ~ Latitude,
lng = ~ Longitude,
popup = fw$Name) %>%
qq
addRasterImage(r, colors = pal, opacity = 0.8)
#A kml conversion using gdal
library("rgdal")
setwd("C:\\Temp")
google = "+init=epsg:3857" #the coordinte system code for google's web mercator projection
ogrListLayers("Roma Rioni.kml") #this gives the list of layers in the kml. Usually it requires some experimentation to get the one that you need
rioni <- readOGR(dsn = "Roma Rioni.kml", layer = "Livello senza titolo")
rioni_google = spTransform(rioni, CRS(google)) #converts it from lat/long to web mercator
writeOGR(rioni_google, dsn="." ,layer="RomeRioni_WebMerc",driver="ESRI Shapefile")