/**
* This file is part of eTracks.
*
* eTracks is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* eTracks is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with eTracks. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.etracks.dades;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.ListIterator;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import org.xmlpull.v1.XmlSerializer;
import android.content.Context;
import android.util.Log;
import android.util.Xml;
import com.etracks.domini.Photo;
import com.etracks.domini.Placemark;
import com.etracks.domini.Punt;
import com.etracks.domini.Route;
/**
* This class handles the creation of kmz.
*
* @author Albert
*
*/
public class KmzAdapter {
/**
* TAG used in the logs.
*/
private static String TAG = "KmzAdapter";
/**
* Temporal folder.
*/
private static String DIR = "tmp";
/**
* The execution context.
*/
private Context ctx;
/**
* Constructor of the class.
*
* @param c
* The execution context.
*/
public KmzAdapter(Context c) {
ctx = c;
}
/**
* Prepare a temporal kmz.
*
* @param r
* Identifier of the Route.
* @return The kmz File.
*/
public File getTmpKmz(Route r) {
File dir = ctx.getDir(DIR, Context.MODE_PRIVATE);
deleteTmpFiles(dir);
String name = r.getName().replace(' ', '_');
File tmpFile = new File(dir + "/" + name +".kmz");
generateKMZ(tmpFile, r);
return tmpFile;
}
/**
* Delete the temporal directory.
*/
public void deleteTmpFile() {
File dir = ctx.getDir(DIR, Context.MODE_PRIVATE);
deleteTmpFiles(dir);
}
/**
* Generate a kmz file of the specified route.
*
* @param kmzFile
* Kmz File.
* @param r
* Route.
*/
public void generateKMZ(File kmzFile, Route r) {
// CtrlMemory cm = new CtrlMemory(ctx);
// Route r = cm.getFullRoute(idr);
byte[] buf = new byte[2048];
try {
kmzFile.createNewFile();
// create zip file
ZipOutputStream zipFile = new ZipOutputStream(new FileOutputStream(
kmzFile));
// add kml
zipFile.putNextEntry(new ZipEntry("doc.kml"));
writeKml(zipFile, r);
zipFile.closeEntry();
// addFiles()
ListIterator<Photo> iter = r.getPhotos().listIterator();
while (iter.hasNext()) {
String p = iter.next().getPath();
if (p != null) {
File f = new File(p);
String name = "files/" + f.getName();
zipFile.putNextEntry(new ZipEntry(name));
FileInputStream in = new FileInputStream(f);
int len;
while ((len = in.read(buf)) > 0) {
zipFile.write(buf, 0, len);
}
zipFile.closeEntry();
}
}
zipFile.flush();
zipFile.close();
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
}
private void writeKml(ZipOutputStream fileos, Route r) {
// we create a XmlSerializer in order to write xml data
XmlSerializer serializer = Xml.newSerializer();
try {
// we set the FileOutputStream as output for the serializer, using
// UTF-8 encoding
serializer.setOutput(fileos, "UTF-8");
// Write <?xml declaration with encoding (if encoding not null) and
// standalone flag (if standalone not null)
serializer.startDocument(null, Boolean.valueOf(true));
// set indentation option
serializer.setFeature(
"http://xmlpull.org/v1/doc/features.html#indent-output",
true);
// start a tag called "kml"
serializer.startTag(null, "kml");
serializer.attribute(null, "xmlns",
"http://www.opengis.net/kml/2.2");
serializer.startTag(null, "Document");
serializer.startTag(null, "name");
serializer.text(r.getName());
serializer.endTag(null, "name");
serializer.startTag(null, "description");
serializer.text(r.getDescription());
serializer.endTag(null, "description");
// Track
serializer.startTag(null, "Placemark");
serializer.startTag(null, "name");
serializer.text(r.getName());
serializer.endTag(null, "name");
serializer.startTag(null, "description");
serializer.text(r.getDescription());
serializer.endTag(null, "description");
serializer.startTag(null, "LineString");
serializer.startTag(null, "extrude");
serializer.text("1");
serializer.endTag(null, "extrude");
serializer.startTag(null, "altitudeMode");
serializer.text("absolute");
serializer.endTag(null, "altitudeMode");
serializer.startTag(null, "coordinates");
serializer.text(prepareCoords(r));
serializer.endTag(null, "coordinates");
serializer.endTag(null, "LineString");
serializer.endTag(null, "Placemark");
// Photos
ListIterator<Photo> li = r.getPhotos().listIterator();
while (li.hasNext()) {
Photo p = li.next();
serializer.startTag(null, "PhotoOverlay");
serializer.startTag(null, "name");
serializer.text(p.getDesc());
serializer.endTag(null, "name");
serializer.startTag(null, "description");
serializer.text(p.getDesc());
serializer.endTag(null, "description");
if (p.getPath() != null) {
serializer.startTag(null, "Icon");
serializer.startTag(null, "href");
File f = new File(p.getPath());
serializer.text("files/" + f.getName());
serializer.endTag(null, "href");
serializer.endTag(null, "Icon");
}
serializer.startTag(null, "Point");
serializer.startTag(null, "coordinates");
serializer.text(p.getLon() + "," + p.getLat() + ","
+ p.getAltitude());
serializer.endTag(null, "coordinates");
serializer.endTag(null, "Point");
serializer.endTag(null, "PhotoOverlay");
}
// PlaceMarks
ListIterator<Placemark> li2 = r.getPlacemarks().listIterator();
while (li2.hasNext()) {
Placemark p = li2.next();
serializer.startTag(null, "Placemark");
serializer.startTag(null, "name");
serializer.text(p.getName());
serializer.endTag(null, "name");
serializer.startTag(null, "description");
serializer.text(p.getDesc());
serializer.endTag(null, "description");
serializer.startTag(null, "Point");
serializer.startTag(null, "coordinates");
serializer.text(p.getLon() + "," + p.getLat() + ","
+ p.getAltitude());
serializer.endTag(null, "coordinates");
serializer.endTag(null, "Point");
serializer.endTag(null, "Placemark");
}
serializer.endTag(null, "Document");
serializer.endTag(null, "kml");
serializer.endDocument();
// write xml data into the FileOutputStream
serializer.flush();
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
// TODO Auto-generated method stub
}
private String prepareCoords(Route r) {
ArrayList<Punt> a = r.getPunts();
StringBuilder s = new StringBuilder();
for (int i = 0; i < a.size(); i++) {
Punt p = a.get(i);
s.append(p.getLon() + "," + p.getLat() + ",");
s.append(p.getAltitude() + "\n");
}
return s.toString();
}
private void deleteTmpFiles(File dir) {
File fs[] = dir.listFiles();
for (int i = 0; i < fs.length; i++)
fs[i].delete();
}
}
|