Skip to content

Commit

Permalink
Merge pull request #42 from missioncommand/#23-Kml-import-export-example
Browse files Browse the repository at this point in the history
#23 kml import export example
  • Loading branch information
JACochran authored Nov 10, 2017
2 parents a60ca90 + 4ecf85e commit 08ef107
Show file tree
Hide file tree
Showing 13 changed files with 1,378 additions and 5 deletions.
1 change: 1 addition & 0 deletions example-kml-exportimport/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
32 changes: 32 additions & 0 deletions example-kml-exportimport/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "26.0.2"
defaultConfig {
applicationId "mil.emp3.example_kml_exportimport"
minSdkVersion 23
targetSdkVersion 23
versionCode 1
versionName "1.0"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

dependencies {
implementation ("mil.army.missioncommand:emp3-android-sdk-view:$version_emp3Android@aar") { transitive = true }
implementation (group: 'mil.army.missioncommand', name: 'emp3-android-sdk-core', version: "$version_emp3Android", ext: 'aar') { transitive = true }

implementation ("com.android.support:appcompat-v7:23.2.1")
}
21 changes: 21 additions & 0 deletions example-kml-exportimport/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
23 changes: 23 additions & 0 deletions example-kml-exportimport/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.example_kml_exportimport">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

<application
android:allowBackup="true"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainKMLActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
package com.example.example_kml_exportimport;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Environment;
import android.os.Looper;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import android.os.Handler;

import org.xmlpull.v1.XmlPullParserException;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.UUID;

import mil.emp3.api.KML;
import mil.emp3.api.Overlay;
import mil.emp3.api.exceptions.EMP_Exception;
import mil.emp3.api.interfaces.ICamera;
import mil.emp3.api.interfaces.IEmpExportToStringCallback;
import mil.emp3.api.interfaces.IFeature;
import mil.emp3.api.interfaces.IMap;
import mil.emp3.api.utils.kml.EmpKMLExporter;

public class MainKMLActivity extends AppCompatActivity
{
private IMap map;
final private HashMap<UUID, IFeature> oFeatureHash = new HashMap<>();
final private Overlay overlay = new Overlay();
private Handler handler;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_kml);
this.map = (IMap) findViewById(R.id.map);

this.handler = new Handler(Looper.getMainLooper());
//setup overlay
this.overlay.setName("Test Overlay");

try
{
this.map.addOverlay(overlay, true);
}
catch (EMP_Exception e)
{
e.printStackTrace();
}
}

/***
* Exports the current map to kml and saves the data on disk
* @param view the view that sent the event
*/
public void exportToKmlExample(final View view)
{
if(!hasRequiredPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE))
{
return;
}

//export the map as a kml file
EmpKMLExporter.exportToString(this.map,
true,
new IEmpExportToStringCallback()
{
@Override
public void exportSuccess(final String kmlString)
{
//Write KML export to a file
final File storagePublicDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
final File destination = new File(storagePublicDirectory, "MapExport.kml");

//ensure that there isn't a file already created at that location
if(destination.exists())
{
destination.delete();
}
//write the kml string to a file
try(FileOutputStream outputStream = new FileOutputStream(destination))
{
final byte[] data = kmlString.getBytes();
outputStream.write(data, 0, data.length);
outputStream.flush();

//notify the user the kml file has been exported
MainKMLActivity.this.makeToast(String.format("Export KML complete. %s", destination.getAbsolutePath()));
}
catch (FileNotFoundException exception)
{
MainKMLActivity.this.makeToast(String.format("Writing KML to file failed. %s", exception.getMessage()));
}
catch (IOException exception)
{
MainKMLActivity.this.makeToast(String.format("Writing KML to file failed. %s", exception.getMessage()));
}
}

@Override
public void exportFailed(final Exception Ex)
{
MainKMLActivity.this.makeToast(String.format("Export to KML failed. %s", Ex.getMessage()));
}
});
}

/**
* Imports the example kml data to the map
* @param view the view that sent the event
*/
public void importKmlExample(final View view)
{
//read the kml sample data
try (final InputStream stream = getApplicationContext().getResources().openRawResource(R.raw.kml_samples))
{
//create a kml feature to add to the map
final KML kmlFeature = new KML(stream);
//add kml to existing overlay on the map
this.overlay.addFeature(kmlFeature, true);

//save instance of kml feature to our hashmap
this.oFeatureHash.put(kmlFeature.getGeoId(), kmlFeature);

//set the camera where the data is at
final ICamera camera = this.map.getCamera();
camera.setAltitude(400);
camera.setLongitude(-122.08447);
camera.setLatitude(37.42198);
camera.apply(true);
}
catch (XmlPullParserException | EMP_Exception | IOException Ex )
{
Toast.makeText(MainKMLActivity.this,
String.format("Importing Kml failed. %s", Ex.getMessage()),
Toast.LENGTH_LONG).show();
}
}

/***
* plots random points to export as kml
* @param view the view that sent the event
*/
public void plotRandomPoints(final View view)
{
try
{
PlotUtility.plotRandomPoints(100, this.map.getCamera(), this.overlay);
PlotUtility.plotRandomUrlPoints(100, this.map.getCamera(), this.overlay);
}
catch (EMP_Exception Ex)
{
Toast.makeText(MainKMLActivity.this,
String.format("Plotting features failed. %s", Ex.getMessage()),
Toast.LENGTH_LONG).show();
}
}

private static final int PERMISSION_REQUEST_CODE = 1;

private boolean hasRequiredPermission(final String permission)
{
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M)
{

if (checkSelfPermission(permission) == PackageManager.PERMISSION_DENIED)
{

Log.d("permission", String.format("permission denied to %s - requesting it", permission));
final String[] permissions = {permission};

requestPermissions(permissions, PERMISSION_REQUEST_CODE);
return false;
}
}
return true;
}

private void makeToast(final String text)
{
handler.post(() -> Toast.makeText(MainKMLActivity.this, text, Toast.LENGTH_LONG).show());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package com.example.example_kml_exportimport;

import android.util.Log;

import org.cmapi.primitives.GeoColor;
import org.cmapi.primitives.GeoFillStyle;
import org.cmapi.primitives.GeoIconStyle;
import org.cmapi.primitives.GeoLabelStyle;
import org.cmapi.primitives.GeoPosition;
import org.cmapi.primitives.GeoStrokeStyle;
import org.cmapi.primitives.IGeoAltitudeMode;
import org.cmapi.primitives.IGeoColor;
import org.cmapi.primitives.IGeoFillStyle;
import org.cmapi.primitives.IGeoIconStyle;
import org.cmapi.primitives.IGeoLabelStyle;
import org.cmapi.primitives.IGeoMilSymbol;
import org.cmapi.primitives.IGeoPosition;
import org.cmapi.primitives.IGeoStrokeStyle;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import armyc2.c2sd.renderer.IconRenderer;
import armyc2.c2sd.renderer.utilities.SymbolUtilities;
import armyc2.c2sd.renderer.utilities.UnitDef;
import armyc2.c2sd.renderer.utilities.UnitDefTable;
import mil.emp3.api.MilStdSymbol;
import mil.emp3.api.Overlay;
import mil.emp3.api.Point;
import mil.emp3.api.Rectangle;
import mil.emp3.api.exceptions.EMP_Exception;
import mil.emp3.api.interfaces.ICamera;
import mil.emp3.api.interfaces.IFeature;
import mil.emp3.api.interfaces.IOverlay;
import mil.emp3.api.utils.EmpGeoColor;

/**
* @author Jenifer Cochran
*/
public class PlotUtility
{
private final static String TAG = MainKMLActivity.class.getSimpleName();


public static void plotRandomPoints(final int numberOfPoints,
final ICamera camera,
final IOverlay overlay) throws EMP_Exception
{
for(int pointCount = 0; pointCount < numberOfPoints; pointCount++)
{
plotPoint(camera, overlay);
}
}

public static void plotRandomUrlPoints(final int numberOfPoints,
final ICamera camera,
final IOverlay overlay) throws EMP_Exception
{
for(int pointCount = 0; pointCount < numberOfPoints; pointCount++)
{
plotUrlPoint(camera, overlay);
}
}

public static void plotPoint(final ICamera camera,
final IOverlay overlay) throws EMP_Exception
{
final Point oPoint = new Point();
final GeoIconStyle pointStyle = new GeoIconStyle();

pointStyle.setSize(50.0);

oPoint.setPosition(getRandomCoordinate(camera));
overlay.addFeature(oPoint, true);
Log.i(TAG, oPoint.toString());
}

public static void plotUrlPoint(final ICamera camera,
final IOverlay overlay) throws EMP_Exception
{
final Point oPoint = new Point();
final IGeoIconStyle oIconStyle = new GeoIconStyle();

//set the style
oIconStyle.setOffSetY(0);
oIconStyle.setOffSetX(20);

//set up the point
oPoint.setIconStyle(oIconStyle);
oPoint.setPosition(getRandomCoordinate(camera));
oPoint.setIconURI("http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png");

overlay.addFeature(oPoint, true);
}


protected static IGeoPosition getRandomCoordinate(ICamera oCamera)
{
IGeoPosition oPos = new GeoPosition();
double dTemp;

dTemp = oCamera.getLatitude() + (3 * Math.random()) - 1.5;
oPos.setLatitude(dTemp);
dTemp = oCamera.getLongitude() + (3 * Math.random()) - 1.5;
oPos.setLongitude(dTemp);
oPos.setAltitude(Math.random() * 16000.0);

return oPos;
}
}
Loading

0 comments on commit 08ef107

Please sign in to comment.