mirror of
				https://github.com/kidwellj/mapping_environmental_action.git
				synced 2025-11-04 01:34:10 +00:00 
			
		
		
		
	removed wip
This commit is contained in:
		
							parent
							
								
									328f58e69a
								
							
						
					
					
						commit
						c24f041a97
					
				
					 2 changed files with 0 additions and 169 deletions
				
			
		| 
						 | 
				
			
			@ -1,103 +0,0 @@
 | 
			
		|||
require(RCurl) # used for fetching reproducible datasets
 | 
			
		||||
require(sf) # new simplefeature data class, supercedes sp in many ways
 | 
			
		||||
# using GEOS 3.6.1, GDAL 2.1.3, PROJ 4.9.3
 | 
			
		||||
require(sp) # needed for proj4string, deprecated by sf()
 | 
			
		||||
require(rgdal) # version version: 1.3-6
 | 
			
		||||
require(rgeos) # used for buffering below
 | 
			
		||||
require(devtools)
 | 
			
		||||
require(ggplot2)
 | 
			
		||||
 | 
			
		||||
# setwd("~/Downloads/test")
 | 
			
		||||
# load data
 | 
			
		||||
 | 
			
		||||
transition_wgs <- read.csv(text=getURL("https://zenodo.org/record/165519/files/SCCAN_1.4.csv"))
 | 
			
		||||
coordinates(transition_wgs) <- c("X", "Y")
 | 
			
		||||
proj4string(transition_wgs) <- CRS(wgs84)
 | 
			
		||||
transition_sp <- spTransform(transition_wgs, bng)
 | 
			
		||||
transition_sf <- st_as_sf(transition, coords = c("X", "Y"), crs=27700)
 | 
			
		||||
 | 
			
		||||
# Download data as ESRI Shapefile from page at: https://gateway.snh.gov.uk/natural-spaces/dataset.jsp?dsid=SSSI
 | 
			
		||||
 | 
			
		||||
unzip("SSSI_SCOTLAND_ESRI.zip")
 | 
			
		||||
 | 
			
		||||
# # Download data as ESRI Shapefile from page at: http://data-forestry.opendata.arcgis.com/datasets/3cb1abc185a247a48b9d53e4c4a8be87_0
 | 
			
		||||
 | 
			
		||||
unzip("National_Forest_Inventory_Woodland_Scotland_2017.zip")
 | 
			
		||||
 | 
			
		||||
sssi_sf <- st_read("./data/SSSI_SCOTLAND.shp")
 | 
			
		||||
sssi_sp <- readOGR("./data", "SSSI_SCOTLAND") 
 | 
			
		||||
 | 
			
		||||
forest_inventory_sf <- st_read("./data/National_Forest_Inventory_Woodland_Scotland_2017.shp")
 | 
			
		||||
forest_inventory_sp <- readOGR("./data", "National_Forest_Inventory_Woodland_Scotland_2017") 
 | 
			
		||||
 | 
			
		||||
# Test validity of geometry
 | 
			
		||||
 | 
			
		||||
rgeos::gIsValid(sssi_sp) 
 | 
			
		||||
valid <- st_is_valid(sssi_sf)
 | 
			
		||||
  valid[valid == FALSE]
 | 
			
		||||
 | 
			
		||||
# Alternative approach from stackexchange using simplified geometry
 | 
			
		||||
  
 | 
			
		||||
  sssi_sp2 <- rgeos::gSimplify(sssi_sp, tol=3)
 | 
			
		||||
  par(mfrow=c(1,2))
 | 
			
		||||
  plot(sssi_sp[1,])
 | 
			
		||||
  plot(sssi_sp2[1,])
 | 
			
		||||
  
 | 
			
		||||
  system.time(
 | 
			
		||||
    sssi_b1000 <- rgeos::gBuffer(sssi_sp2, width = 20, quadsegs = 30)
 | 
			
		||||
  )  
 | 
			
		||||
  
 | 
			
		||||
  sssi_sf2 <- sf::st_simplify(sssi_sf)
 | 
			
		||||
  system.time(
 | 
			
		||||
    sssi_b1000 <- sf::st_buffer(sssi_sf2, dist = 20)
 | 
			
		||||
  ) 
 | 
			
		||||
  
 | 
			
		||||
# Render ggplot2 plot on simplified geometry
 | 
			
		||||
 | 
			
		||||
ggplot(sssi_sf2) + geom_sf(aes(fill = PA_CODE))
 | 
			
		||||
plot(sssi_sf2)
 | 
			
		||||
 | 
			
		||||
# Plot
 | 
			
		||||
 | 
			
		||||
pdf(file = "sssi_polygons.pdf", width=5, height=6, res=300, units="in")
 | 
			
		||||
plot(st_geometry(sssi_simple), col='#aaaaaa', border=0)
 | 
			
		||||
dev.off()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
# First test out plots using spatialfeatures and spdf with core R
 | 
			
		||||
 | 
			
		||||
system.time(
 | 
			
		||||
plot(sssi_sf2)
 | 
			
		||||
  )
 | 
			
		||||
 | 
			
		||||
system.time(
 | 
			
		||||
  plot()
 | 
			
		||||
  )
 | 
			
		||||
 | 
			
		||||
system.time(
 | 
			
		||||
plot(sssi_sp)
 | 
			
		||||
  )
 | 
			
		||||
 | 
			
		||||
# First test out plots using spatialfeatures and spdf with ggplot2
 | 
			
		||||
 | 
			
		||||
system.time(
 | 
			
		||||
  ggplot() +
 | 
			
		||||
    geom_sf(data = sssi_sf)
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
system.time(
 | 
			
		||||
  ggplot() +
 | 
			
		||||
    geom_polygon(data = sssi_sp)
 | 
			
		||||
)
 | 
			
		||||
# Now try to run a count within a buffer:
 | 
			
		||||
 | 
			
		||||
st_crs(sssi_sf) <- 27700
 | 
			
		||||
st_crs(transition_sf) <- 27700
 | 
			
		||||
 | 
			
		||||
# CRS uses meters for units, so buffer here should be a modest 50m:
 | 
			
		||||
 | 
			
		||||
count_data_sf <- sum(apply(st_within(points_sf, st_buffer(sssi, dist = 50), sparse=FALSE), 1, any))
 | 
			
		||||
 | 
			
		||||
# count_data_sf <- sum(apply(gWithin(points_sf, gBuffer(sssi,width=50)
 | 
			
		||||
  
 | 
			
		||||
sessioninfo::session_info()
 | 
			
		||||
| 
						 | 
				
			
			@ -1,66 +0,0 @@
 | 
			
		|||
# Plot SSSI polygons with ECS points (using sp, for now)
 | 
			
		||||
 | 
			
		||||
ggplot() + 
 | 
			
		||||
  geom_polygon(aes(x = long, y = lat, group = group), 
 | 
			
		||||
               data = sssi_sp, 
 | 
			
		||||
               colour = 'black', 
 | 
			
		||||
               alpha = .7, 
 | 
			
		||||
               size = .005) + 
 | 
			
		||||
  geom_point(aes(X, Y, fill = NULL, group = NULL), size = 1, data=ecs_df, 
 | 
			
		||||
             colour = "black", 
 | 
			
		||||
             fill = "white",
 | 
			
		||||
             size = .15,
 | 
			
		||||
             stroke = .002,
 | 
			
		||||
             alpha = .6, 
 | 
			
		||||
             show.legend = TRUE) +
 | 
			
		||||
  labs(x = NULL, y = NULL, fill = "Groups",
 | 
			
		||||
       title = "Figure 11", 
 | 
			
		||||
       subtitle="Sites of Special Scientific Interest with points marked",
 | 
			
		||||
       caption = paste("Jeremy H. Kidwell :: jeremykidwell.info",
 | 
			
		||||
                       "Data: UK Data Service (OGL) & Jeremy H. Kidwell",
 | 
			
		||||
                       "You may redistribute this graphic under the terms of the CC-by-SA 4.0 license.",
 | 
			
		||||
                       sep = "\n"))
 | 
			
		||||
 | 
			
		||||
# # ggplot using sf seems to be severely broken for now. Commenting out and reverting to sp
 | 
			
		||||
# if (utils::packageVersion("ggplot2") > "2.2.1")
 | 
			
		||||
#   ggplot() + geom_sf(data = sssi) +
 | 
			
		||||
#   geom_point(aes(X, Y, fill = NULL, group = NULL), size = 1, data=ecs_df, 
 | 
			
		||||
#              colour = "black", 
 | 
			
		||||
#              fill = "white",
 | 
			
		||||
#              size = .3,
 | 
			
		||||
#              stroke = .1,
 | 
			
		||||
#              show.legend = FALSE) +
 | 
			
		||||
#   labs(x = NULL, 
 | 
			
		||||
#        title = "Figure 11", 
 | 
			
		||||
#        subtitle="Sites of Special Scientific Interest with points marked",
 | 
			
		||||
#        caption = paste("Jeremy H. Kidwell :: jeremykidwell.info",
 | 
			
		||||
#                        "Data: UK Data Service (OGL) & Jeremy H. Kidwell",
 | 
			
		||||
#                        "You may redistribute this graphic under the terms of the CC-by-SA 4.0 license.",
 | 
			
		||||
#                        sep = "\n"))
 | 
			
		||||
 | 
			
		||||
# Plot Forest Inventory
 | 
			
		||||
 | 
			
		||||
ggplot() + 
 | 
			
		||||
  geom_polygon(aes(x = long, y = lat, group = group), 
 | 
			
		||||
               data = forest_inventory_sp, 
 | 
			
		||||
               colour = 'black', 
 | 
			
		||||
               alpha = .7, 
 | 
			
		||||
               size = .005) + 
 | 
			
		||||
  geom_point(aes(X, Y, fill = NULL, group = NULL), size = 1, data=ecs_df, 
 | 
			
		||||
             colour = "black", 
 | 
			
		||||
             fill = "white",
 | 
			
		||||
             size = .15,
 | 
			
		||||
             stroke = .002,
 | 
			
		||||
             alpha = .6, 
 | 
			
		||||
             show.legend = TRUE) +
 | 
			
		||||
  labs(x = NULL, y = NULL, fill = "Groups",
 | 
			
		||||
       title = "Figure 11", 
 | 
			
		||||
       subtitle="Sites of Special Scientific Interest with points marked",
 | 
			
		||||
       caption = paste("Jeremy H. Kidwell :: jeremykidwell.info",
 | 
			
		||||
                       "Data: UK Data Service (OGL) & Jeremy H. Kidwell",
 | 
			
		||||
                       "You may redistribute this graphic under the terms of the CC-by-SA 4.0 license.",
 | 
			
		||||
                       sep = "\n"))
 | 
			
		||||
 | 
			
		||||
# # ggplot using sf seems to be severely broken for now. Commenting out and reverting to sp
 | 
			
		||||
# ggplot() +
 | 
			
		||||
#   geom_sf(data = forest_inventory)
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue