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).
Related
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.
Probably a very basic question but I found nothing in the documentation of Simple Features R package.
I'm looking for the native sf function to extract on the fly all the columns of an sf object without the geometries. Just like SP#data with sp objects.
The following function does the job but I would prefer to use a native function :
st_data <- function(SF) { SF[, colnames(SF) != attr(SF, "sf_column"), drop = TRUE]}
A typical use is when I want to merge two sf dataset by attribute (merge does not work with two sf objects) : merge(SF1, st_data(SF2)).
In that case it would be impractical to use st_geometry(SF2) <- NULL because it does not work "on the fly" and I don't want to permanently drop the geometry column and SF2[,1:5,drop=T] is impractical too because I have to look into the object to see where the geometry column is.
Using : sf_0.5-4 - R 3.4.1
We can use the st_geometry<- function and set the geometry to be NULL.
library(sf)
nc <- st_read(system.file("shape/nc.shp", package="sf"))
nc_df <- `st_geometry<-`(nc, NULL)
class(nc_df)
[1] "data.frame"
As you can see, nc_df is a dataframe now, so I think you can do the following for your example.
merge(SF1, `st_geometry<-`(SF2, NULL))
Update
As Gilles pointed out, another function, st_set_geometry, can also achieve the same task. It is probably a better choice since using st_set_geometry does not need the use of "``" and "<-" to enclose the st_geometry function.
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
I follow this document and this question.
I have downloaded ne_10m_admin_1_states_provinces files but I can not find
country_code_index (for Iran Or other country) in it.
I use this command in ubuntu command line :
ogrinfo ne_10m_admin_1_states_provinces.dbf -al > Out.txt
out.txt's content is :
How to find --country_code_index AND --country_name_index ?
Go through Map-converter-notes which has very good information about all the steps required to generate a new map from public data.
Linux users can install a very handy tool SAGA-GIS to generate required shape file and then use converter.py to generate map to use with jvectormap.com plugin.
country_code_index is the index of attribute which will be used as code name for specific region on map and country_name_index is the attribute which will be shown as label of region on map. Attributes chosen for these should have unique values in map data table.
I am using QGIS software. I would like to show value of each raster cell as label.
My idea (I don't know any plugin or any functionality from QGIS which allow to it easier) is to export raster using gdal2xyz.py into coordinates-value format and then save it as vector (GML or shapefile). For this second task, I try to use
*gdal_polygonize.py:*
gdal_polygonize.py rainfXYZ.txt rainf.shp Creating output rainf.shp of
format GML.
0...10...20...30...40...50...60...70...80...90...100 - done.
unfortunately I am unable to load created file (even if I change the extension to .gml)
ogr2ogr tool don't even recognize this format.
yes - sorry I forgot to add such information.
In general after preparing CSV file (using gdal2xyz.py with -csv option),
I need to add one line at begining of it:
"Longitude,Latitude,Value" (without the quotes)
Then I need to create a VRT file which contain
*> <OGRVRTDataSource>
> <OGRVRTLayer name="Shapefile_name">
> <SrcDataSource>Shapefile_name.csv</SrcDataSource>
> <GeometryType>wkbPoint</GeometryType>
>
> <GeometryField encoding="PointFromColumns" x="Longitude"
> y="Latitude"/>
> </OGRVRTLayer> </OGRVRTDataSource>*
Run the command "ogr2ogr -select Value Shapefile_name.shp Shapefile_name.vrt". I got the file evap_OBC.shp and two other associated files.
For the sake of archive completeness, this question has also been asked on GDAL mailing list as thread save raster as point-vector file. It seems Chaitanya provided solution for it.