KML Goodness

http://drewk.net/NetBeansProjects.zip

/*
* Writting a KML facility to help with FCS debugging.
*/
package gekml;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

/**
* < ?xml version="1.0" encoding="UTF-8"?>
*
* * Simple placemark
* Attached to the ground. Intelligently places itself
* at the height of the underlying terrain.

* * -122.0822035425683,37.42228990140251,0
*
*
*

* @author drew
* @author Andrew Kaluzniacki : drewk.net
* @copyright Andrew Kaluzniacki
*/
public class KmlFile {

private KmlFile() {

// empty
}

public static KmlFile create() {
return new KmlFile();
}
private ArrayList placemarkList = new ArrayList();

public void addPlacemark(double longitude, double latitude, String name, String description) {
placemarkList.add(new Placemark(longitude, latitude, 0, name, description));
}

public void saveAs(String filePath) {

FileOutputStream out = null;
try {
out = new FileOutputStream(filePath);
} catch (FileNotFoundException ex) {
ex.printStackTrace();
throw new IllegalArgumentException(”SaveAs must have valid filePath:” + filePath);
}
PrintWriter pw = new PrintWriter(out);

pw.print(”< ?xml version=\"1.0\" encoding=\"UTF-8\"?>“);
pw.print(” “);

for (int i = 0; i < placemarkList.size(); i++) {
pw.print(((Placemark) placemarkList.get(i)).toXmlString());
}

pw.print(" “);

if (pw.checkError()) {
System.err.println(”Error in ” + pw.toString());
}

pw.close();
try {
out.close();
} catch (IOException ex) {
ex.printStackTrace();
throw new IllegalStateException(”Error in KmlFile::SaveAs()”);
} finally {
try {
out.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}

——–

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
* Andrew Kaluzniacki
*
*/

/**
* Two key items:
* Placemarks - used to show aimpoints
* Polygon - used to show the target area
*
* Start:
* If I can just get plaecmarks working, I can fake target areas.
* The key is to get the addPlacemark(location, name)
*
*
* References:
* http://code.google.com/apis/kml/documentation/kml_tut.html#descriptive_html
*
*
*
* < ?xml version="1.0" encoding="UTF-8"?>
*
* * Simple placemark
* Attached to the ground. Intelligently places itself
* at the height of the underlying terrain.

* * -122.0822035425683,37.42228990140251,0
*
*
*

*
*
*/
package gekml;

/**
*
* @author drew
*/
public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {

// Create a nice kml file
KmlFile kf = KmlFile.create();

// 32°25′34.65″N
// 106°40′13.68″W
kf.addPlacemark( -106.6253642521367, 32.35612182602554, “AndrewK”, “Is Success”);
kf.saveAs(”my.kml”);
}
}

——-
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package gekml;

/**
* * * Simple placemark
* Attached to the ground. Intelligently places itself
* at the height of the underlying terrain.

* * -122.0822035425683,37.42228990140251,0
*
*
* @author drew
* @author Andrew Kaluzniacki : drewk.net
* @copyright Andrew Kaluzniacki
*/
public class Placemark {

private double longitude;
private double latitude;
/** relative to ground - o is at ground level */
private double altitude;
private String name = “Untitled Placemark”;
private String description = “”;

Placemark(double longitude, double latitude, double altitude, String name, String description)
{
this.longitude = longitude;
this.latitude = latitude;
this.altitude = altitude;
if ( name != null ) {
this.name = name;
}
if ( description != null ) {
this.description = description;
}
}

/** Write to stream as xml
*
*/
String toXmlString() {
StringBuffer buf = new StringBuffer();
buf.append(” \n”);
buf.append(” “).append(name).append(”\n”);
buf.append(” “).append(description).append(”\n”);
buf.append(” \n”);
buf.append(” “).append(longitude).append(’,');
buf.append(latitude).append(’,').append(altitude).append(”
\n”);
buf.append(”
\n”);
buf.append(”
\n”);
return buf.toString();
}
}

Leave a Reply

You must be logged in to post a comment.