Skip to content

Commit

Permalink
GeoJSON load and save.
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] committed Mar 12, 2014
1 parent a80b96d commit c19a43d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
5 changes: 4 additions & 1 deletion OSMBonusPackDemo/res/menu/option_menu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@
android:title="Edit..."/>
<item
android:id="@+id/menu_kml_save"
android:title="Save"/>
android:title="Save as KML"/>
<item
android:id="@+id/menu_geojson_save"
android:title="Save as GeoJSON"/>
<item
android:id="@+id/menu_kml_clear"
android:title="Clear"/>
Expand Down
27 changes: 21 additions & 6 deletions OSMBonusPackDemo/src/com/osmbonuspackdemo/MapActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ public void onClick(View v) {
Intent onCreateIntent = getIntent();
if (onCreateIntent.getAction().equals(Intent.ACTION_VIEW)){
String uri = onCreateIntent.getDataString();
getKml(uri, true);
openFile(uri, true);
}
}
}
Expand Down Expand Up @@ -937,7 +937,7 @@ void openKMLUrlDialog(){
ed.putString("KML_URI", uri);
ed.commit();
dialog.cancel();
getKml(uri, false);
openFile(uri, false);
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
Expand All @@ -948,14 +948,17 @@ void openKMLUrlDialog(){
builder.show();
}

void getKml(String uri, boolean onCreate){
void openFile(String uri, boolean onCreate){
//TODO: use an Async task
mKmlDocument = new KmlDocument();
boolean ok;
if (uri.startsWith("file:/")){
uri = uri.substring("file:/".length());
File file = new File(uri);
ok = mKmlDocument.parseFile(file);
if (uri.endsWith(".json"))
ok = mKmlDocument.parseGeoJSON(file);
else //assume KML
ok = mKmlDocument.parseFile(file);
} else {
ok = mKmlDocument.parseUrl(uri);
}
Expand All @@ -980,6 +983,14 @@ void saveKml(String fileName){
Toast.makeText(this, "Unable to save "+fileName, Toast.LENGTH_SHORT).show();
}

void saveGeoJSON(String fileName){
boolean result = mKmlDocument.saveAsGeoJSON(mKmlDocument.getDefaultPathForAndroid(fileName));
if (result)
Toast.makeText(this, fileName + " saved", Toast.LENGTH_SHORT).show();
else
Toast.makeText(this, "Unable to save "+fileName, Toast.LENGTH_SHORT).show();
}

void updateUIWithKml(){
if (kmlOverlay != null)
map.getOverlays().remove(kmlOverlay);
Expand Down Expand Up @@ -1135,7 +1146,7 @@ else if (map.getTileProvider().getTileSource() == MAPBOXSATELLITELABELLED)
case R.id.menu_kml_file:
//TODO : openKMLFileDialog();
File file = mKmlDocument.getDefaultPathForAndroid("current.kml");
getKml("file:/"+file.toString(), false);
openFile("file:/"+file.toString(), false);
return true;
case R.id.menu_kml_get_overlays:
insertOverlaysInKml();
Expand All @@ -1150,9 +1161,13 @@ else if (map.getTileProvider().getTileSource() == MAPBOXSATELLITELABELLED)
startActivityForResult(myIntent, KML_TREE_REQUEST);
return true;
case R.id.menu_kml_save:
//TODO: openKMLSaveDialog();
//TODO: openSaveDialog();
saveKml("current.kml");
return true;
case R.id.menu_geojson_save:
//TODO: openSaveDialog();
saveGeoJSON("current.json");
return true;
case R.id.menu_kml_clear:
mKmlDocument = new KmlDocument();
updateUIWithKml();
Expand Down

0 comments on commit c19a43d

Please sign in to comment.