{"word_count": 331, "character_count": 4159}
As I discover the marvels of (geo)R world, I found out this article (link)It explains how to generate KML files from meuse, the source dataset delivered with sp package, using R software.In the tutorial, KML files are created from scracth, writing each line of it.OGR driver only gives access to the options name and description (rdgal driver). So, "brute coding" may be necessary if you want to control other elements: color, heights for extruded objects...Many people would like to visualize some buildings in 3D in google. The reference data that we have for buildings is often plain but there usually is a column specifying the height of each building. For example, for the french BDTOPO, the HAUTEUR column.On the web, I found an arcScript for this but no opensource pre-built script or software.Reading the article of the above mentioned tutorial called ploting 3D points gave me the idea to develop an R script for this purpose - which wasn't so difficult, indeed.Here is the complete script:# import rgdal library which contains all necessary driverslibrary(rgdal)&#160;&#160;# shp import&#160;buildings&#60;-readOGR(dsn="C:/buildings.shp",layer="buildings")&#160;&#160;#variable initialization&#160;zColumn&#60;-"HEIGHT"&#160;&#160;filename&#60;-"buildings.kml"&#160;&#160;#header writing&#160;write(paste("&#60;?xml&#160;version="1.0" encoding="ISO-8859-1" ?&#62;&#60;kml&#160;xmlns="http:#www.opengis.net/kml/2.2"&#62;&#60;Document&#62;&#60;Folder&#62;&#60;name&#62;buildingsge&#60;/name&#62;&#60;Schema&#160;name="buildingsge" id="buildingsge"&#62;&#60;/Schema&#62;",&#160;sep=""), filename)&#160;&#160;#objects creation through a loop&#160;for (x in 1:length(buildings@polygons)) {&#160;&#160;write(paste("&#60;Placemark&#62;&#60;name&#62;",&#160;buildings@data$ID[x],&#160;"&#60;/name&#62;&#60;Style&#62;&#60;LineStyle&#62;&#60;color&#62;7fbcbcbc&#60;/color&#62;&#60;/LineStyle&#62;&#60;PolyStyle&#62;&#60;color&#62;7fbcbcbc&#60;/color&#62;&#60;/PolyStyle&#62;&#60;/Style&#62;&#60;ExtendedData&#62;&#60;SchemaData&#160;schemaUrl="#buildingsge"&#62;&#60;SimpleData name="nature"&#62;",&#160;buildings@data$NATURE[x],"&#60;/SimpleData&#62;&#60;/SchemaData&#62;&#60;/ExtendedData&#62;&#60;Polygon&#62;&#60;extrude&#62;1&#60;/extrude&#62;&#60;altitudeMode&#62;relativeToGround&#60;/altitudeMode&#62;&#60;outerBoundaryIs&#62;&#60;LinearRing&#62;&#60;coordinates&#62;",&#160;sep=""), filename, append=TRUE)&#160;&#160;# polygon coordinates injection&#160;coordsPol&#60;-paste(buildings@polygons[[x]]@Polygons[[1]]@coords[,1],&#160;buildings@polygons[[x]]@Polygons[[1]]@coords[,2], buildings@data[[zColumn]][x],&#160;sep=",")&#160;write(coordsPol, filename, append=TRUE)&#160;&#160;write("&#60;/coordinates&#62;&#60;/LinearRing&#62;&#60;/outerBoundaryIs&#62;&#60;/Polygon&#62;&#60;/Placemark&#62;",filename,append=TRUE)}&#160;&#160;# tail writing after exiting the loop&#160;write("&#60;/Folder&#62;&#60;/Document&#62;&#60;/kml&#62;",filename,append=TRUE)Created by Pretty R at inside-R.orgNotes:Here, I preferred to condense all the elements (&#60;Placemark&#62;, &#60;coordinates&#62;, etc...) in one line. Yet, it's not optimized for reading. One may prefer individualizing each line by as many write() occurrences as elements like in the spatial analyst tuto.writingwrite("text",filename, append=TRUE)is used to write the file from scratchpaste functionIt's used in order to concatenate several string elements&#160; Placemark namebuildings@data$ID[x]each bulding is identified by its ID as extracted from the buildings shapefile.&#160;Coordinatesbuildings@polygons[[x]]@Polygons[[1]]@coords[,1] is the X coordinate of the xth polygon buildings@polygons[[x]]@Polygons[[1]]@coords[,2] is the Ybuildings@data[[zColumn]][x] accesses the zColumn value for the xth polygon/building. Additional info&#60;ExtendedData&#60;SchemaDataschemaUrl="#buildingsge"&#62;"nature"&#62;",buildings@data$NATURE[x],"will pop up the NATURE column value when clicking on an element (church, city hall, ...)If you'd like to understand the functions of some KML elementsn check the KML reference guide:http://code.google.com/intl/fr/apis/kml/documentation/kmlreference.html