From f167525f4295b3d0beb3c7b190d81a25de182423 Mon Sep 17 00:00:00 2001 From: Koen Rabaey Date: Tue, 6 May 2014 22:17:52 +0200 Subject: [PATCH] import keyhole markup language files --- OsmAnd/AndroidManifest.xml | 6 + .../osmand/plus/activities/MapActivity.java | 124 ++++++++++++------ .../src/net/osmand/plus/helpers/Kml2Gpx.java | 42 ++++++ .../src/net/osmand/plus/helpers/kml2gpx.xslt | 63 +++++++++ 4 files changed, 194 insertions(+), 41 deletions(-) create mode 100644 OsmAnd/src/net/osmand/plus/helpers/Kml2Gpx.java create mode 100644 OsmAnd/src/net/osmand/plus/helpers/kml2gpx.xslt diff --git a/OsmAnd/AndroidManifest.xml b/OsmAnd/AndroidManifest.xml index 170eb4238c..35b33d9dc3 100644 --- a/OsmAnd/AndroidManifest.xml +++ b/OsmAnd/AndroidManifest.xml @@ -78,6 +78,12 @@ + + + + + + diff --git a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java index 0ef56678c0..bf313580a0 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java @@ -1,7 +1,9 @@ package net.osmand.plus.activities; +import java.io.ByteArrayInputStream; import java.io.File; +import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; @@ -36,6 +38,7 @@ import net.osmand.plus.Version; import net.osmand.plus.activities.search.SearchActivity; import net.osmand.plus.base.FailSafeFuntions; import net.osmand.plus.base.MapViewTrackingUtilities; +import net.osmand.plus.helpers.Kml2Gpx; import net.osmand.plus.render.RendererRegistry; import net.osmand.plus.resources.ResourceManager; import net.osmand.plus.routing.RoutingHelper; @@ -340,7 +343,14 @@ public class MapActivity extends AccessibleActivity { final String scheme = data.getScheme(); if ("file".equals(scheme)) { - showImportedGpx(data.getPath()); + if (data.getPath().endsWith("kml")) + { + showImportedKml(new File(data.getPath())); + } + else + { + showImportedGpx(new File(data.getPath())); + } } else if("google.navigation".equals(scheme) || "osmand.navigation".equals(scheme)) { @@ -592,7 +602,7 @@ public class MapActivity extends AccessibleActivity { mapLayers.updateLayers(mapView); mapView.setComplexZoom(mapView.getZoom(), mapView.getSettingsZoomScale()); app.getDaynightHelper().startSensorIfNeeded(new StateChangedListener() { - + @Override public void stateChanged(Boolean change) { getMapView().refreshMap(true); @@ -720,47 +730,79 @@ public class MapActivity extends AccessibleActivity { getMapView().refreshMap(); } - private void showImportedGpx(final String gpxFile) - { - new AsyncTask() - { - ProgressDialog progress = null; + private void showImportedGpx(final File gpxFile) { + new AsyncTask() { + ProgressDialog progress = null; - @Override - protected void onPreExecute() - { - progress = ProgressDialog.show(MapActivity.this, getString(R.string.loading), getString(R.string.loading_data)); - } + @Override + protected void onPreExecute() { + progress = ProgressDialog.show(MapActivity.this, getString(R.string.loading), getString(R.string.loading_data)); + } - @Override - protected GPXFile doInBackground(Void... nothing) - { - return GPXUtilities.loadGPXFile(getMyApplication(), new File(gpxFile)); - } + @Override + protected GPXFile doInBackground(Void... nothing) { + return GPXUtilities.loadGPXFile(getMyApplication(), gpxFile); + } - @Override - protected void onPostExecute(GPXFile result) - { - progress.dismiss(); - if (result != null) - { - if (result.warning != null) - { - AccessibleToast.makeText(MapActivity.this, result.warning, Toast.LENGTH_LONG).show(); - } - else - { - getMyApplication().setGpxFileToDisplay(result, true); - final WptPt moveTo = result.findPointToShow(); - if (moveTo != null) - { - mapView.getAnimatedDraggingThread().startMoving(moveTo.lat, moveTo.lon, mapView.getZoom(), true); - } - mapView.refreshMap(); - } + @Override + protected void onPostExecute(GPXFile result) { + progress.dismiss(); + if (result != null) { + if (result.warning != null) { + AccessibleToast.makeText(MapActivity.this, result.warning, Toast.LENGTH_LONG).show(); + } else { + getMyApplication().setGpxFileToDisplay(result, true); + final WptPt moveTo = result.findPointToShow(); + if (moveTo != null) { + mapView.getAnimatedDraggingThread().startMoving(moveTo.lat, moveTo.lon, mapView.getZoom(), true); + } + mapView.refreshMap(); + } - } - } - }.execute(); - } + } + } + }.execute(); + } + + private void showImportedKml(final File kmlFile) { + new AsyncTask() { + ProgressDialog progress = null; + + @Override + protected void onPreExecute() { + progress = ProgressDialog.show(MapActivity.this, getString(R.string.loading), getString(R.string.loading_data)); + } + + @Override + protected GPXFile doInBackground(Void... nothing) { + final String result = Kml2Gpx.toGpx(kmlFile); + if (result == null) { + return null; + } + try { + return GPXUtilities.loadGPXFile(getMyApplication(), new ByteArrayInputStream(result.getBytes("UTF-8"))); + } catch (UnsupportedEncodingException e) { + return null; + } + } + + @Override + protected void onPostExecute(GPXFile result) { + progress.dismiss(); + if (result != null) { + if (result.warning != null) { + AccessibleToast.makeText(MapActivity.this, result.warning, Toast.LENGTH_LONG).show(); + } else { + getMyApplication().setGpxFileToDisplay(result, true); + final WptPt moveTo = result.findPointToShow(); + if (moveTo != null) { + mapView.getAnimatedDraggingThread().startMoving(moveTo.lat, moveTo.lon, mapView.getZoom(), true); + } + mapView.refreshMap(); + } + + } + } + }.execute(); + } } diff --git a/OsmAnd/src/net/osmand/plus/helpers/Kml2Gpx.java b/OsmAnd/src/net/osmand/plus/helpers/Kml2Gpx.java new file mode 100644 index 0000000000..8f69d14015 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/helpers/Kml2Gpx.java @@ -0,0 +1,42 @@ +package net.osmand.plus.helpers; + +import android.annotation.TargetApi; +import net.osmand.PlatformUtil; +import org.apache.commons.logging.Log; + +import javax.xml.transform.*; +import javax.xml.transform.stream.StreamResult; +import javax.xml.transform.stream.StreamSource; +import java.io.File; +import java.io.StringWriter; + +/** + * @author Koen Rabaey + */ +public class Kml2Gpx { + + public static final Log LOG = PlatformUtil.getLog(Kml2Gpx.class); + + @TargetApi(8) + public static String toGpx(final File kml) { + try { + final Source xmlSource = new StreamSource(kml); + final Source xsltSource = new StreamSource(Kml2Gpx.class.getResourceAsStream("kml2gpx.xslt")); + + final StringWriter sw = new StringWriter(); + + TransformerFactory.newInstance().newTransformer(xsltSource).transform(xmlSource, new StreamResult(sw)); + + return sw.toString(); + + } catch (TransformerConfigurationException e) { + LOG.error(e.toString(), e); + } catch (TransformerFactoryConfigurationError e) { + LOG.error(e.toString(), e); + } catch (TransformerException e) { + LOG.error(e.toString(), e); + } + + return null; + } +} diff --git a/OsmAnd/src/net/osmand/plus/helpers/kml2gpx.xslt b/OsmAnd/src/net/osmand/plus/helpers/kml2gpx.xslt new file mode 100644 index 0000000000..a09c910373 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/helpers/kml2gpx.xslt @@ -0,0 +1,63 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file