How to load a geospatial pdf in R? - r
I am new handling spatial data, and newer doing it in R.
My last attemp was trying to read data in a geographical pdf format. It is information about mexican political bourdaries, so polygons, the file.
I tryied to use the rgdal package to read the data. After typing ogrDrivers()[40:45,], which show the drivers available, I got.
name write
40 PCIDSK TRUE
41 PDF TRUE
42 PDS FALSE
43 PGDump TRUE
44 PGeo FALSE
45 PostgreSQL TRUE
The result show that there is a driver for PDF's, but trying the usual way to read files readOGR(dsn = "data source name", layer = "LAYER") produces:
Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = use_iconv, :
Cannot open file
The help of the function does not say the values expected neither for dsn nor for layer when the file is in a geospatial pdf format.
Does anybody knows a way to import data from a pdf? this is from geospatial format; I would appreciate any answer.
By the way, I have Ubuntu 14.04.3 with Qgis installed, and the latest versions of R and rgeos.
The dsn is the file path, and the layer name is internal to the PDF. You can get a list of layers with ogrListLayers on the file name:
> ogrListLayers("foo.pdf")
[1] "polys"
attr(,"driver")
[1] "PDF"
attr(,"nlayers")
[1] 1
Ugly output, but that's one layer called polys. So I can read it like this:
> polys = readOGR("./foo.pdf","polys")
OGR data source with driver: PDF
Source: "./foo.pdf", layer: "polys"
with 9 features
It has 1 fields
Note that this only applies to a special class of PDF files with the map data stored in a particular way. Just because your PDF has a map in it, doesn't make it a Geospatial PDF. Here's the command line test on my Geospatial PDF:
$ ogrinfo Monaco/foo.pdf
Had to open data source read-only.
INFO: Open of `Monaco/foo.pdf'
using driver `PDF' successful.
1: polys (Polygon)
and here's the test on yours:
$ ogrinfo CED06_CARTA_110614.pdf
FAILURE:
Unable to open datasource `CED06_CARTA_110614.pdf' with the following drivers.
-> ESRI Shapefile
-> MapInfo File
[etc etc]
-> PDF
[etc etc]
So you dont have a Geospatial PDF.
Your options are, in possible order of simplicity, something like:
Get the boundary data in a GIS data format (eg shapefile, GeoPDF)
Save as an image, load into a GIS, georeference and trace it (QGIS can do this)
Get the raw PDF vectors out of the PDF, assuming they are vectors (first glance shows me the map isn't an image), then find the right transformation to whatever coordinate system, then possibly rebuild the topology if all you have is line segments...
I've had a little bit of success using pstoedit to convert the PDF to a DXF file which can be loaded into QGIS, but then you have to clean it up and reconstruct the polygons, and then its still not in the right geographical location. It would be much simpler if you can get a shapefile of the regions you are interested in.
If what you want is a raster version of the PDF, then you can either use raster::stack("file.pdf") or readGDAL("file.pdf"). But you'll get an image with no georeferencing (it'll just have a bounding box of the number of pixels) since there's no coordinate system with the PDF.
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)
plot VIIRS raster data (.h5) in R
Im looking into how to handle .h5 (hdf) VIIRS DNB raster data in R. I would like to plot it or export it as a geotiff. So far I am able to read in the file with hdf<- h5file("mypath/GDNBO-SVDNB_npp_d20171101_t0110370_e0116174_b31151_c20180208224859630066_nobc_ops.h5", mode = "r") But I cannot figure out how to access the "radiance" band, let alone plot it. I have read the "h5" package documentation without any progress.. As another option I have tried to export the h5 file to geotiff with sds <- get_subdatasets("subset_1_d20171101_t0110370.h5") gdal_translate(sds[17], dst_dataset = "radiance.tif", overwrite=T) #subset 17 is the radiance band i need however the file has lost its geolocation during the transformation, when the geotiff is opened in e.g. QGIS, it doesnt have any location or projection. Anyone knows how to deal with these type of files in R?
How to convert rds format data into shp format in R?
I have a map data whose format is rds. Now I want to use this data in another software which asks for shp format. How to convert rds format data into shp format in R?
If it is spatial object saved as a R-specific binary file of "Serialization Interface for Single Objects" type (see ?readRDS) probably created at some point by saveRDS(), read your file with library(rgdal) library(sp) x <- readRDS("path/to/the/rds_file.rds") and then write it with: rgdal::writeOGR(x, "path/to/destination", "filename", driver = "ESRI Shapefile") Be sure not to put ".shp" at the end of your output filename. Also be sure not to put a / at the end of the destination folder. Otherwise you might face the error Creation of output file failed When the error Error: inherits(obj, "Spatial") is not TRUE you might have forgotten the x as the first argument in the writeOGR function.
Error using fortify() in ggplot2
I am creating a map using the zip code information from the census data together with data that I have for each zip code. I obtained the shapefiles for the zip codes from https://www.census.gov/geo/maps-data/data/tiger-cart-boundary.html. I successfully read in the shapefiles using readOGR with the following code: Zip = readOGR("C:\\...pathname....\\cb_2013_us_zcta510_500k.shp") I am running into a problem when I try to use fortify so that I can combine the information from the shapefile with my data. names(Zip) [1] "ZCTA5CE10" "AFFGEOID10" "GEOID10" "ALAND10" "AWATER10" test = ggplot2::fortify(Zip, region="AWATER10") Error in fortify.SpatialPolygonsDataFrame(Zip, region = "AWATER10") : `region' not supported The error is replicated when I try all of the results from names(Zip). I am not sure which column contains the zip code information that I am interested in, so I just tried all of them. My ultimate goal is to use the zip code information with ggmap to fill in the zip codes with different colors based on information in my data.
Creating Shapefiles in R
I'm trying to create a shapefile in R that I will later import to either Fusion Table or some other GIS application. To start,I imported a blank shapefile containing all the census tracts in Canada. I have attached other data (in tabular format) to the shapefile based on the unique ID of the CTs, and I have mapped my results. At the moment, I only need the ones in Vancouver and I would like to export a shapefile that contains only the Vancouver CTs as well as my newly attached attribute data. Here is my code (some parts omitted due to privacy reasons): shape <- readShapePoly('C:/TEST/blank_ct.shp') #Load blank shapefile shape#data = data.frame(shape#data, data2[match(shape#data$CTUID, data2$CTUID),]) #data2 is my created attributes that I'm attaching to blank file shape1 <-shape[shape$CMAUID == 933,] #selecting the Vancouver CTs I've seen other examples using this: writePolyShape to create the shapefile. I tried it, and it worked to an extent. It created the .shp, .dbf, and .shx files. I'm missing the .prj file and I'm not sure how to go about creating it. Are there better methods out there for creating shapefiles? Any help on this matter would be greatly appreciated.
Use rgdal and writeOGR. rgdal will preserve the projection information something like library(rdgal) shape <- readOGR(dsn = 'C:/TEST', layer = 'blank_ct') # do your processing shape#data = data.frame(shape#data, data2[match(shape#data$CTUID, data2$CTUID),]) #data2 is my created attributes that I'm attaching to blank file shape1 <-shape[shape$CMAUID == 933,] writeOGR(shape1, dsn = 'C:/TEST', layer ='newstuff', driver = 'ESRI Shapefile') Note that the dsn is the folder containing the .shp file, and the layer is the name of the shapefile without the .shp extension. It will read (readOGR) and write (writeOGR) all the component files (.dbf, .shp, .prj etc)
Problem solved! Thank you again for those who help! Here is what I ended up doing: As Mnel wrote, this line will create the shapefile. writeOGR(shape1, dsn = 'C:/TEST', layer ='newstuff', driver = 'ESRI Shapefile') However, when I ran this line, it came back with this error: Can't convert columns of class: AsIs; column names: ct2,mprop,mlot,mliv This is because my attribute data was not numeric, but were characters. Luckily, my attribute data is all numbers so I ran transform() to fix this problem. shape2 <-shape1 shape2#data <- transform(shape1#data, ct2 = as.numeric(ct2), mprop = as.numeric(mprop), mlot = as.numeric(mlot), mliv = as.numeric(mliv)) I tried the writeOGR() command again, but I still didn't get the .prj file that I was looking for. The problem was I didn't specified the coordinate systems for the shapefile when I was importing the file. Since I already know what the coordinate system is, all I had to do was define it when importing. readShapePoly('C:/TEST/blank_ct.shp',proj4string=CRS("+proj=longlat +datum=WGS84") After that, I re-ran all the things I wanted to do with the shapefile, and the writeOGR line for exporting. And that's it!