2

I have >500 very large KMLs that contains only point data and a reference to a .png pop-up

Each KML looks like this

<kml xmlns:xsd="http://schemas.opengis.net/kml/2.2.0/ogckml22.xsd" xmlns:xmlns="http://www.opengis.net/kml/2.2/" version="1.0">
  <Document>
    <name>test.kml</name>
    <open>1</open>
    <Folder>
      <name>TS</name>
      <Style id="pnt1">
        <LabelStyle>
          <scale>0.3</scale>
        </LabelStyle>
        <BalloonStyle>
          <text>$[description]</text>
        </BalloonStyle>
      </Style>
      <Placemark>
        <name>1</name>
        <styleUrl>#pnt1</styleUrl>
        <description><![CDATA[<img src="TS_1.png" height="400" width="1200" align ="middle" />]]></description>
        <Point>
          <extrude>1</extrude>
          <altitudeMode>clampToGround</altitudeMode>
          <coordinates>-117.766574236,56.204770715,0</coordinates>
        </Point>
      </Placemark>

    </Folder>
  </Document>
</kml>

The file above contains only one point, but most of my files contain more than 1 million equally spaced points.

I can do the regionation in Google Earth Pro, but for >500 KMLs, it will take very long time and effort.

Is there a way to do similar regionation in R?

How to define the dimensions of a regions and how to cascade them in R?

I used R to write the KMLs in the first place using the following code:

  write_header <- function (outfile) {

  cat('<kml xmlns:xsd="http://schemas.opengis.net/kml/2.2.0/ogckml22.xsd" xmlns:xmlns="http://www.opengis.net/kml/2.2/" version="1.0">\n')
  cat("  <Document>\n")
  cat(paste0(
      "    <name>",outfile,"</name>\n"))
  cat("    <open>1</open>\n")
  cat("    <Folder>\n")
  cat("      <name>TS</name>\n")
}

write_styles <- function(df){

  for (j in 1: nrow(df)){
    cat(paste0(
        '      <Style id="pnt', j,'">\n'))
    cat("        <LabelStyle>\n")
    cat("          <scale>0.3</scale>\n")
    cat("        </LabelStyle>\n")
    cat("        <IconStyle>\n")
    cat("          <color>#ff000000</color>\n")
    cat("          <scale>0.3</scale>\n")
    cat("      </Style>\n")
  }
}

write_placeholders <- function(df){

  for (k in 1: nrow(df)){
    row=df[k,]
    x=row$x
    y=row$y
    cat("      <Placemark>\n")
    cat(paste0(
        "        <name>", k, "</name>\n"))
    cat(paste0(
        "        <styleUrl>#pnt",k, "</styleUrl>\n"))
    cat(paste0(
        '        <description><![CDATA[<img src="TS_', k, '.png" height="400" width="1200" align ="middle" />]]></description>\n'))
    cat("        <Point>\n")
    cat(paste0(
        "          <extrude>",k,"</extrude>\n"))
    cat("          <altitudeMode>clampToGround</altitudeMode>\n")
    cat(paste0(
        "          <coordinates>",x,",",y,",",0,"</coordinates>\n"))
    cat("        </Point>\n")
    cat("      </Placemark>\n")
  }
}

write_end <- function(){

  cat("    </Folder>\n")
  cat("  </Document>\n")
  cat("</kml>\n")
}

writ_TS2kml <- function(outDir,df, outfile) {

  setwd(outDir)
  tryCatch({
    sink(outfile)
    write_header(outfile)
    write_styles(df)
    write_placeholders(df)
    write_end()
    sink()
  }, error=function(e){cat("ERROR :",conditionMessage(e), "\n")})}
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.