[R-sig-Geo] how to show polygons from SHP file in Google map?

Jim Burke j.burke at earthlink.net
Thu Jun 4 15:59:47 CEST 2009


Jian,

First realize where your R code was trying to
find your shapefile. In your code below you
are searching maptools\shapes and not your code
subdirectory. Maptools is the domain of that
package and you should not put your shapefiles
there but in your own separate user subdirectory.

natSHP.sp <- readShapePoly(system.file("nro2.shp", package="maptools")[1], proj4string=CRS("+init=epsg:2166"))

Then in R, make sure R is pointing to the
directory where your nro2.shp file exists.

This can be done by selecting in R, File,
Change Directory.

Good luck,
Jim Burke

Torleif Markussen Lunde wrote:
> Yes
>
> First: type ?readShapePoly
> If it does not help, use Google, Bing, RSiteSearch, etc.
> Third: ask R-sig-geo
>
> natSHP.sp <- readShapePoly("nro2.shp", proj4string=CRS("+init=epsg:2166"))
> spplot(natSHP.sp, "NATURRAUMO")
>
> To understand the error, try:
> system.file("package="maptools")
> system.file("shapes/sids.shp", package="maptools")
> and
> system.file("nro2.shp", package="maptools")
>
> Best
> Torleif
>
> On Thursday 04 June 2009 03:02:44 pm Jian Li wrote:
>   
>> Dear Torleif,
>>
>> I just tried to produce another SpatialPolygons like nat.sp from SHP file
>> like this following: natSHP.sp <- readShapePoly(system.file("nro2.shp",
>> package="maptools")[1], proj4string=CRS("+init=epsg:2166"))
>>
>> in order to approach it in the same way for saving the information in KML
>> file
>>
>> But it doesn't work. Do you know what's going wrong or do you have any
>> better idea?
>>
>> The nro2.shp is under
>> http://jaguar.biologie.hu-berlin.de/~jian/phpMysqlGoogle/arcViewNro2/
>> available.
>>
>> greeting,
>> Jian
>>
>>     
>>> ########################
>>> ##Export e00 to Google Earth (png)##
>>> ########################
>>>
>>> #1: import using RArcInfo
>>>
>>> #Set the working directory to where you stored your nat.e00 data
>>> setwd('~/e00data') #Use your own path
>>>
>>> #load the RArcInfo library (for functions e00toavc, get.xxx)
>>> require(RArcInfo)
>>>
>>> #Convert the e00 to SpatialPolygons
>>> e00toavc('nat.e00', './nat2')
>>> nat.arc <- get.arcdata("./", "nat2")
>>> nat.pal <- get.paldata("./", "nat2")
>>> require(maptools) #Needed for pal2SpatialPolygons
>>> nat.sp <- pal2SpatialPolygons(nat.arc,
>>> 	      nat.pal,
>>> 	      nat.pal[[1]]$PolygonId[-1],
>>> 	      dropPoly1=TRUE,
>>> 	      proj4string=CRS("+init=epsg:2166"))
>>>
>>> #Transform to longlat, WGS84
>>> require(rgdal)
>>> nat2.sp <- spTransform(nat.sp, CRS("+proj=longlat +ellps=WGS84"))
>>>
>>> #Set up metadata
>>> GSG <- GE_SpatialGrid(nat2.sp)
>>>
>>> #Ask for the tempdir
>>> tmp.dir <- tempdir()
>>>
>>> #To change the resolution (and line width), change res
>>> res <- 5
>>> #Make png-file
>>> png(file=paste(tmp.dir, "/", "nat.png", sep=""), width=GSG$width*res,
>>> height=GSG$height*res,
>>> bg="transparent")
>>>      par(mar=c(0,0,0,0), xaxs="i", yaxs="i")
>>>      plot(nat2.sp, xlim=GSG$xlim, ylim=GSG$ylim, lwd = res)
>>> dev.off()
>>>
>>> #And make the Google earth kml-file
>>> kmlOverlay(SGqk, paste(tmp.dir, "/",  "nat.kml", sep=""),
>>> paste(tmp.dir, "/", "nat.png", sep=""))
>>>
>>> #Now, see in td, and you have your kml and png.
>>>
>>>
>>> ###########################################
>>> ##Export the same data as polygons (still to Google earth)
>>> #The second option is to export as polygons
>>> #(See ?kmlPolygon in maptools)
>>>
>>> out <- sapply(slot(nat2.sp, "polygons"), function(x) { kmlPolygon(x,
>>>     name=paste(slot(x, "ID")),
>>>     col="transparent", lwd=5, border='black',
>>>     description=paste(slot(x, "ID"))) })
>>>
>>> tf <- tempfile()
>>>
>>> kmlFile <- file(paste(tf, ".kml", sep=""), "w")
>>> cat(kmlPolygon(kmlname="MyGPoly",
>>> kmldescription="<i>Masterthesis</i>")$header,
>>>     file=kmlFile, sep="\n")
>>> cat(unlist(out["style",]), file=kmlFile, sep="\n")
>>> cat(unlist(out["content",]), file=kmlFile, sep="\n")
>>> cat(kmlPolygon()$footer, file=kmlFile, sep="\n")
>>> close(kmlFile)
>>> #################
>>>
>>> Best wishes
>>> Torleif
>>>
>>> On Wednesday 03 June 2009 09:45:45 am Paul Hiemstra wrote:
>>>       
>>>> Hi,
>>>>
>>>> You have to do two things, read your data and export it to a format for
>>>> Google Earth.
>>>>
>>>> The Cran Spatial Taskview suggests the RArcInfo package to read the e00
>>>> files into R. I'm not sure into what format they are loaded. Maybe
>>>> readOGR from the rgdal package can also read e00 files, depending on
>>>> the GDAL/OGR version you have available.
>>>>
>>>> If you have your vector file in an R session in a SpatialPolygons
>>>> object (see the sp-package), the following code shows an example how to
>>>> export the data to a format that Google Earth can read:
>>>>
>>>> library(rgdal)
>>>> data(meuse)
>>>> coordinates(meuse) = ~x+y
>>>> proj4string(meuse) = CRS("+init=epsg:28992")
>>>> proj4string(meuse)
>>>> meuse.ll = spTransform(meuse, CRS("+proj=longlat"))
>>>> writeOGR(meuse.ll, "meuse.kml", "meuse.kml", driver="KML")
>>>>
>>>> I'm not sure if the RArcInfo reads the e00 file into a SpatialPolygons
>>>> object (Virgillio?), or if it is easy to convert the output from
>>>> RArcInfo to an sp-class. rgdal always reads data into an sp-class, but
>>>> I'm not sure if this supports the e00 format.
>>>>
>>>> I hope this e-mail provides you with some clues to work with,
>>>>
>>>> cheers and good luck with your thesis,
>>>> Paul
>>>>
>>>> Jian Li wrote:
>>>>         
>>>>> Dear r-sig-geo Members,
>>>>>
>>>>> recently, I've been trying to display the information from a e00 file
>>>>> (Grauss-Krüger coordinatensystem) in Google map but until now no
>>>>> success.
>>>>>
>>>>> I would be very appreciated if anyone can help me to solve the
>>>>> problem which directly concerns my master thesis.
>>>>>
>>>>> e00 file is under
>>>>> http://jaguar.biologie.hu-berlin.de/~jian/phpMysqlGoogle/nat.e00
>>>>> available
>>>>>
>>>>> Thank you in Advanced
>>>>>
>>>>> Best Regards,
>>>>> Jian
>>>>>
>>>>> check out the rest of the Windows Live™.
>>>>> More than mail–Windows Live™ goes way beyond your inbox.
>>>>>  More than messages
>>>>> _________________________________________________________________
>>>>> Show them the way! Add maps and directions to your party invites.
>>>>> http://www.microsoft.com/windows/windowslive/products/events.aspx
>>>>> 	[[alternative HTML version deleted]]
>>>>>
>>>>>
>>>>> ---------------------------------------------------------------------
>>>>> ---
>>>>>
>>>>> _______________________________________________
>>>>> R-sig-Geo mailing list
>>>>> R-sig-Geo at stat.math.ethz.ch
>>>>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>>>>           
>> _________________________________________________________________
>> Invite your mail contacts to join your friends list with Windows Live
>> Spaces. It's easy!
>> http://spaces.live.com/spacesapi.aspx?wx_action=create&wx_url=/friends.aspx
>> &mkt=en-us
>>     
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>



More information about the R-sig-Geo mailing list