use of org.geotools.kml.KMLConfiguration in project spatial-portal by AtlasOfLivingAustralia.
the class ExportLayerComposer method exportAreaAs.
public void exportAreaAs(String type, String name, SelectedArea sa) {
String exportBaseDir = CommonData.getSettings().getProperty(StringConstants.ANALYSIS_OUTPUT_DIR) + File.separator + "export" + File.separator;
try {
String id = String.valueOf(System.currentTimeMillis());
File shpDir = new File(exportBaseDir + id + File.separator);
shpDir.mkdirs();
File shpfile;
String contentType = LayersUtil.LAYER_TYPE_ZIP;
String outfile = name.replaceAll(" ", "_");
if ("shp".equals(type)) {
shpfile = new File(exportBaseDir + id + File.separator + outfile + "_Shapefile.shp");
ShapefileUtils.saveShapefile(shpfile, sa.getWkt(), name);
outfile += "_SHP.zip";
} else if ("kml".equals(type)) {
StringBuilder sbKml = new StringBuilder();
sbKml.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>").append("\r");
sbKml.append("<kml xmlns=\"http://earth.google.com/kml/2.2\">").append("\r");
sbKml.append("<Document>").append("\r");
sbKml.append(" <name>Spatial Portal Active Area</name>").append("\r");
sbKml.append(" <description><![CDATA[Active area saved from the ALA Spatial Portal: http://spatial.ala.org.au/]]></description>").append("\r");
sbKml.append(" <Style id=\"style1\">").append("\r");
sbKml.append(" <LineStyle>").append("\r");
sbKml.append(" <color>40000000</color>").append("\r");
sbKml.append(" <width>3</width>").append("\r");
sbKml.append(" </LineStyle>").append("\r");
sbKml.append(" <PolyStyle>").append("\r");
sbKml.append(" <color>73FF0000</color>").append("\r");
sbKml.append(" <fill>1</fill>").append("\r");
sbKml.append(" <outline>1</outline>").append("\r");
sbKml.append(" </PolyStyle>").append("\r");
sbKml.append(" </Style>").append("\r");
sbKml.append(" <Placemark>").append("\r");
sbKml.append(" <name>").append(name).append("</name>").append("\r");
sbKml.append(" <description><![CDATA[<div dir=\"ltr\">").append(name).append("<br></div>]]></description>").append("\r");
sbKml.append(" <styleUrl>#style1</styleUrl>").append("\r");
// Remove first line of kmlGeometry, <?xml...>
Geometry geom = new WKTReader().read(sa.getWkt());
Encoder encoder = new Encoder(new KMLConfiguration());
encoder.setIndenting(true);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
encoder.encode(geom, KML.Geometry, baos);
String kmlGeometry = new String(baos.toByteArray());
sbKml.append(kmlGeometry.substring(kmlGeometry.indexOf("?>") + 2));
sbKml.append(" </Placemark>").append("\r");
sbKml.append("</Document>").append("\r");
sbKml.append("</kml>").append("\r");
shpfile = new File(exportBaseDir + id + File.separator + outfile + "_KML.kml");
BufferedWriter wout = new BufferedWriter(new FileWriter(shpfile));
wout.write(sbKml.toString());
wout.close();
outfile += "_KML.zip";
} else if (StringConstants.WKT.equals(type)) {
shpfile = new File(exportBaseDir + id + File.separator + outfile + "_WKT.txt");
BufferedWriter wout = new BufferedWriter(new FileWriter(shpfile));
wout.write(sa.getWkt());
wout.close();
outfile += "_WKT.zip";
}
// zip shpfile
Zipper.zipDirectory(exportBaseDir + id + File.separator, exportBaseDir + id + ".zip");
try {
byte[] bytes = FileUtils.readFileToByteArray(new File(exportBaseDir + id + ".zip"));
Filedownload.save(bytes, contentType, outfile);
} catch (Exception e) {
LOGGER.error("failed to download file : " + exportBaseDir + id + ".zip", e);
}
try {
remoteLogger.logMapAnalysis(name, "Export - " + StringUtils.capitalize(type) + " Area", sa.getWkt(), "", "", "", outfile, "download");
} catch (Exception e) {
LOGGER.error("remote logger error", e);
}
} catch (Exception e) {
LOGGER.error("Unable to export user area", e);
}
}
Aggregations