how to add my own shape file trajLevel() in openair package R - r

I am using this following code for plotting the trajectories...........
library(openair)
load("GDASNDL1000m.Rdata")
trajLevel(traj,method="hexbin",col="jet",xbin=40,parameters=NULL,
orientation=c(90,0,0),projection="mercator")
result https://github.com/adeckmyn/maps/files/2667752/GDASNDL1000m.zip
Here, I would like to change the base world map with my own shape file.
my shape file is follows....
z1=maptools::readShapePoly("/home/sateeshm/shapefiles/ncmrwf/india_map")
library(maps)
map(z1)
https://github.com/adeckmyn/maps/files/2667336/World-India.zip
#
Now, the actual question is how to link z1 to trajLevel?

To avoid the hard-coded call to "world" in openair, you wil have to create a new world database in the same file based format as that of the "maps" package.
Probably the simplest way to do this, is to use the mapMaker package. This package is not on CRAN but can be found on github. It is the package I used to create the standard world map. The documentation is minimal, but if you don't care about polygon names etc, you can create a "quick and dirty" world map as follows:
# get your new map as a simple list of polygons (or lines)
z1=maps::map(maptools::readShapePoly("india_map"), plot=FALSE)
# create internal representation
z2=mapMaker::map.make(z1)
# write binary files:
mapMaker::map.export.bin(z2, "/my/path/to/world")
# To make map() call this new database:
library(maps)
worldMapEnv="MYMAP"
Sys.setenv("MYMAP"="/my/path/to/") # don't add the "world" !
now map("world") will draw your version of the world map.

Related

Map xml data from arc GIS

Is it possible to plot/map xml data from arc GIS in R? I have an XML file with a basemap I would like to map in R.
I dont know how to add my XML file to my question but if someone could show me how to plot using this type of file that would be awesome. I usually make my maps in r using ggplot and geom_sf.
#How I loaded my XML file into r
# Load the package required to read XML files.
library("XML")
# Also load the other required package.
library("methods")
# Give the input file name to the function.
result <- xmlParse(file = "Grey background.xml")
# Print the result.
print(result)

Is there an issue with how I'm reading in my shapefile and plotting it?

I've tried to read in my shape file and then plot it but it seems as though RStudio is stuck on something and will not finish running the plot function. Right now I have:
library(rgdal)
new_county_path <- paste(county_path, "tl_2014_us_county.shp",sep='/')
county1 <- readOGR(new_county_path)
plot(county1)
But it doesn't produce a plot and it seems to be continuously be stuck on something as it only says
>plot(county1)
in the R console. Am I doing something wrong to cause this and is there a better way to read and plot shapefiles?
Without a reproducible example, I can't say why your code doesn't work. If you have a valid shapefile, you should be able to read it in and plot it using the code that you provided:
# first get and save a shapefile to make the code easily reproducible
library(sf)
nc <- st_read(system.file("shape/nc.shp", package="sf"))
st_write(obj = nc, dsn = 'test/nc.shp')
# now there's a shapefile named "nc.shp" saved in the "test" folder.
# the functions you're using will work on a valid path to a valid shapefile:
library(rgdal)
nc1 <- readOGR("test/nc.shp")
plot(nc1)
I typically use the sf package, which provides more versatility (particularly for plotting):
library(sf)
nc2 <- st_read("test/nc.shp")
plot(st_geometry(nc2))
If your code doesn't work, there's likely a problem with either the path you provided to the readOGR function or with the shapefile itself.

adding custom map to maps package

I want to be able to use map.where function on a map that is not currently available in the MapEnv in the maps package--such as these maps of Brazil: http://www.usp.br/nereus/?dados=brasil. They go into a more granular level than what is available in the maps package.
Is there anyway to add them to the package data so that they can be used by maps.where?
Yes, it is usually possible to load shp files into map(). You will need extra packages to read the shp files first, though. Also, you will have to know the name of the field that names the polygons.
For instance, using one of the maps from your link:
> ufebrasil <- rgdal::readOGR("UFEBRASIL.shp")
> names(ufebrasil)
[1] "ID" "CD_GEOCODU" "NM_ESTADO" "NM_REGIAO"
> mymap=maps::SpatialPolygons2map(ufebrasil, namefield="NM_ESTADO")
> map.where(mymap, -48.6, -26.46)
[1] "SANTA CATARINA:1"
You can also simply call
mymap=maps::map(ufebrasil, namefield="NM_ESTADO")
to plot a map and give the same map data as above (map() will call SpatialPolygons2map automatically if necessary).

How to load *part* of a multifeature geojson file in R?

I have a geojson that is a FeatureCollection containing 2 geographic data types: a LineString and a waypoint - see the raw file here - this is how it looks on GitHub:
I want to load only only the LineString, so this is what I do:
library(RCurl)
obj <- getURL("https://raw.githubusercontent.com/Robinlovelace/stplanr/master/inst/extdata/route_data.geojson")
writeLines(obj, "/tmp/obj.geojson")
obj <- readLines("/tmp/obj.geojson")
just_lines <- obj[14:(length(obj) - 28)]
just_lines[1] <- paste0("{", just_lines[1])
just_lines[length(just_lines)] <- "}"
writeLines(just_lines, "/tmp/just_lines.geojson")
Now we have removed the pesky lines at the beginning and end of the file, it's a nicely formed GeoJSON file that we can load and plot, yay:
library(rgdal)
route <- readOGR("/tmp/just_lines.geojson", layer = "OGRGeoJSON")
plot(route)
Except it should be obvious to any R user that this is a very clunky and inefficient way of doing this involving too many lines of code and unnecessary reading and writing to the hard disc. There must be another way!
Options I've looked at
geojsonio
jsonlite
leaflet, which can display the FeatureCollection but seemingly not extract its parts.
Context
I'm creating a package for sustainable transport planning, stplanr. A function to find cycling routes (like in the image below) needs to load in the FeatureCollection geojson data from the CycleStreets.net api.
Read the data using jsonlite direct from the URL:
obj <- jsonlite::fromJSON("https://raw.githubusercontent.com/Robinlovelace/stplanr/master/inst/extdata/route_data.geojson")
Convert the first object in the collection to SpatialLines:
sl = SpatialLines(list(Lines(list(Line(obj$features[1,]$geometry$coordinates[[1]])),ID=1)))
plot(sl)
That assumes the feature is a single line string.
To make a SpatialLinesDataFrame with the attributes:
sldf=SpatialLinesDataFrame(sl=sl,data=obj$features[1,]$properties)
Should probably also give it a CRS:
proj4string(sldf)=CRS("+init=epsg:4326")
I don't know if this is possible in LeafletR but Leaflet's L.GeoJSON layer has a filter method which can render (or not render) a collection's features based on the properties the feature has. Some code:
L.geoJson(geojson, {
'filter': function (feature) {
return feature.geometry.type === 'LineString'
}
});
An example: http://plnkr.co/edit/RXIO0X?p=preview
Reference: http://leafletjs.com/reference.html#geojson-filter

Using shapefile as an input to the user defined function using R

I have a script to create a randomly distributed square polygons in KML format which takes in shapefile with a single polygon as an input which works absolutely well. The problem arise when I tried to create the user defined function out of it. I used readShapePoly() function to read the shapefile and it works well when used out of the function. But when the function is created in which shapefile should be given as an input, it simply wont take. It shows this error message
Error in getinfo.shape(filen) : Error opening SHP file
I avoid writing extensions and I do have all the extension files to create the shapefile.
Part of the script to read the shapefile using it as the input file:
library(maptools)
library(sp)
library(ggplot2)
Polytokml <- function(shapefile_name){
###Input Files
file1 <- "shapefile_name"
Shape_file <- readShapePoly(file1) #requires maptools
return(Shape_file)
}
The function is created but it doesn't work if the function is called.
>Polytokml(HKH.shp)
Error in getinfo.shape(filen) : Error opening SHP file
This works well out of the function.
###Input Files
file1 <- "shapefile.shp"
Shape_file <- readShapePoly(file1) #requires maptools
This is just an example out of the whole script in which different arguments are taken as an input. So just to make things simple I have added script to read the shapefile which has been a problem now. Do let me know if it is not clear.
Thank you so much in advance :)

Resources