DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • Top 10 Pillars of Zero Trust Networks
  • Getting Started With the YugabyteDB Managed REST API
  • Operator Overloading in Java
  • How To Use Pandas and Matplotlib To Perform EDA In Python

Trending

  • Top 10 Pillars of Zero Trust Networks
  • Getting Started With the YugabyteDB Managed REST API
  • Operator Overloading in Java
  • How To Use Pandas and Matplotlib To Perform EDA In Python
  1. DZone
  2. Coding
  3. Languages
  4. R GIS: Function to Reverse KML Paths

R GIS: Function to Reverse KML Paths

Kay Cichini user avatar by
Kay Cichini
·
May. 06, 14 · Interview
Like (0)
Save
Tweet
Share
2.59K Views

Join the DZone community and get the full member experience.

Join For Free

This is a function I wrote up for reversing KML-paths. The paths within a KML can partially matched by their name-tags.


## name:          ReverseKmlPath  
## use:           Reverser a KML-path by matching its
## arguments:     PATH_TO_DOC the path to the KML-file
##                NAME the value of the name tag, function uses partial matching!
##                'Trail_xyz' will be matched by 'Trail'
## requirements:  KML-structure with Placemarks containing a <name> and a <coordinates> tag
## author:        Kay Cichini
## date:          01-05-2014
## license:       CC-BY-NC-SA
 
ReverseKmlPath <- function(PATH_TO_DOC, NAMES) {
     
    require(XML)
 
    doc <- xmlInternalTreeParse(PATH_TO_DOC)
     
    if (xmlNamespaceDefinitions(doc)[[1]]$uri == "http://www.opengis.net/kml/2.2") {
        namespaces <- c(kml = "http://www.opengis.net/kml/2.2")
        flag <- 1
    } else {
        if (xmlNamespaceDefinitions(doc)[[1]]$uri == "http://earth.google.com/kml/2.0") {
                namespaces <- c(kml0 = "http://earth.google.com/kml/2.0")
                flag <- 0
            } else {
                stop ("Stopped!: Check namespace issue..")
            }
    }
         
     
    for (NAME in NAMES) {
         
        if (flag) {
              query <- paste0("//kml:Placemark[contains(kml:name,'", sprintf("%s", NAME), "'", ")]//kml:coordinates")
          } else {
              query <- paste0("//kml0:Placemark[contains(kml0:name,'", sprintf("%s", NAME), "'", ")]//kml0:coordinates")
          }
 
        coords <- tryCatch(getNodeSet(doc, query, namespaces),
                           error = function(e) message(paste("\nError: *", NAME, "* was NOT successfully matched\n")))
         
        for (i in length(coords)) {
 
            #grab coordinates from node and reverse order
            rev_coord_vector <- rev(unlist(strsplit(gsub("\\t|\\n", "", xmlValue(coords[[i]])), "\\s")))
            rev_coord_string <- paste(rev_coord_vector, collapse = " ")
 
            # re-insert reversed line-string:
            xmlValue(coords[[i]]) <- rev_coord_string
 
            # message
            if (flag) {
                  query <- paste0("//kml:Placemark[contains(kml:name,'", sprintf("%s", NAME), "'", ")]//kml:name")
              } else {
                  query <- paste0("//kml0:Placemark[contains(kml0:name,'", sprintf("%s", NAME), "'", ")]//kml0:name")
            }
            match <- xmlValue(getNodeSet(doc, query, namespaces)[[i]])
            message(paste0("matched name: ", match, "\n..."))
 
        }
    }
 
    # save:
    message("Reversed paths saved to:")
    saveXML(doc, paste0(dirname(PATH_TO_DOC), "/reversed_", basename(PATH_TO_DOC)),
            prefix = newXMLCommentNode("This file was created with the R-package XML::saveXML, see: "))
}
 
## not run:
tf <- tempfile(fileext = ".kml")
download.file("http://dev.openlayers.org/releases/OpenLayers-2.13.1/examples/kml/lines.kml", tf, mode = "wb")
ReverseKmlPath( PATH_TO_DOC = tf, NAMES = c("Absolute", "Relative") )
 
shell.exec(tf)
shell.exec(paste0(dirname(tf), "/reversed_", basename(tf)))
</coordinates></name>

 

R (programming language)

Published at DZone with permission of Kay Cichini, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • Top 10 Pillars of Zero Trust Networks
  • Getting Started With the YugabyteDB Managed REST API
  • Operator Overloading in Java
  • How To Use Pandas and Matplotlib To Perform EDA In Python

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: