Last active
August 29, 2015 14:26
-
-
Save robertoestivill/535830283df8d721b8c9 to your computer and use it in GitHub Desktop.
Object to KML
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.google.gson.Gson; | |
import com.google.gson.JsonElement; | |
import com.google.gson.JsonObject; | |
import java.io.File; | |
import java.io.FileReader; | |
import java.io.FileWriter; | |
import java.io.IOException; | |
import java.util.LinkedList; | |
import java.util.List; | |
public class KmlWriter { | |
public static void main(String[] args) throws IOException { | |
List<Marker> stations = loadItems("stations"); | |
List<Marker> hotels = loadItems("hotels"); | |
toKml(hotels, stations); | |
} | |
static class Marker { | |
double lat; | |
double lon; | |
String title; | |
String comment; | |
String icon; | |
} | |
private static List<Marker> loadItems(String path) throws IOException { | |
File file = new File(path); | |
FileReader reader = new FileReader(file); | |
Gson gson = new Gson(); | |
JsonElement doc = gson.fromJson(reader, JsonElement.class); | |
JsonElement payload = doc.getAsJsonObject().get("payload"); | |
List<Marker> items = new LinkedList<>(); | |
for (int id = 1; id < 10000; id++) { | |
JsonElement each = payload.getAsJsonObject().get(String.valueOf(id)); | |
if (each != null) { | |
JsonObject object = each.getAsJsonObject(); | |
JsonObject fields = object.get("field").getAsJsonObject(); | |
Marker newItem = new Marker(); | |
newItem.lat = object.get("lat").getAsDouble(); | |
newItem.lon = object.get("lon").getAsDouble(); | |
newItem.title = object.get("title").getAsString(); | |
newItem.comment = safe(fields.get("jr_ciudad"), fields.get("jr_direccion")); | |
newItem.icon = object.get("icon").getAsString(); | |
items.add(newItem); | |
} | |
} | |
reader.close(); | |
return items; | |
} | |
private static void toKml(List<Marker> stations, List<Marker> hotels) throws IOException { | |
File output = new File("/Users/rober/Desktop/acas.kml"); | |
output.createNewFile(); | |
FileWriter writer = new FileWriter(output); | |
writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); | |
writer.write("<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n"); | |
writer.write(" <Document>"); | |
String[] icons = new String[]{"aca", "campingsite", "hotel_0star", "lodging_0star", "motel-2"}; | |
for (String icon : icons) { | |
writer.write(" <Style id=\"" + icon + "\">"); | |
writer.write(" <IconStyle>\n"); | |
writer.write(" <Icon>\n"); | |
writer.write(" <href>http://miraargentina.com/components/com_jreviews_addons/geomaps/icons/" + icon + ".png</href>\n"); | |
writer.write(" </Icon>\n"); | |
writer.write(" </IconStyle>\n"); | |
writer.write(" </Style>\n"); | |
} | |
for (Marker station : stations) { | |
writer.write(" <Placemark>\n"); | |
writer.write(" <name>" + station.title + "</name>\n"); | |
writer.write(" <description>" + station.comment + "</description>\n"); | |
writer.write(" <styleUrl>" + station.icon + "</styleUrl>\n"); | |
writer.write(" <Point>\n"); | |
writer.write(" <coordinates>" + station.lon + "," + station.lat + ",0</coordinates>\n"); | |
writer.write(" </Point>\n"); | |
writer.write(" </Placemark>\n"); | |
} | |
for (Marker hotel : hotels) { | |
writer.write(" <Placemark>\n"); | |
writer.write(" <name>" + hotel.title + "</name>\n"); | |
writer.write(" <description>" + hotel.comment + "</description>\n"); | |
writer.write(" <styleUrl>" + hotel.icon + "</styleUrl>\n"); | |
writer.write(" <Point>\n"); | |
writer.write(" <coordinates>" + hotel.lon + "," + hotel.lat + ",0</coordinates>\n"); | |
writer.write(" </Point>\n"); | |
writer.write(" </Placemark>\n"); | |
} | |
writer.write(" </Document>"); | |
writer.write("</kml>\n"); | |
writer.flush(); | |
writer.close(); | |
} | |
private static String safe(JsonElement... objs) { | |
StringBuilder sb = new StringBuilder(); | |
for (JsonElement je : objs) { | |
if (je != null) { | |
String value = je.getAsString(); | |
value = value.substring(value.indexOf("\">") + 2, value.indexOf("</")); | |
sb.append(value.trim()).append(", "); | |
} | |
} | |
String result = sb.toString(); | |
return result.substring(0, result.length() - 2); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
http://miraargentina.com/aca/mapa-sucursales |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment