Skip to content

Commit

Permalink
Mapsforge Reverse Geocoding #383 (#398)
Browse files Browse the repository at this point in the history
* Mapsforge Reverse Geocoding #383

* Mapsforge Reverse Geocoding example #383
  • Loading branch information
devemux86 authored Sep 10, 2017
1 parent 095ed65 commit 5c2b404
Show file tree
Hide file tree
Showing 16 changed files with 887 additions and 36 deletions.
1 change: 1 addition & 0 deletions docs/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- Render themes: line symbol [#124](https://github.com/mapsforge/vtm/issues/124)
- Render themes: stroke dash array [#131](https://github.com/mapsforge/vtm/issues/131)
- POI Search example [#394](https://github.com/mapsforge/vtm/issues/394)
- Mapsforge Reverse Geocoding [#383](https://github.com/mapsforge/vtm/issues/383)
- Core utilities [#396](https://github.com/mapsforge/vtm/issues/396)
- Mapsforge fix artifacts zoom > 17 [#231](https://github.com/mapsforge/vtm/issues/231)
- vtm-theme-comparator module [#387](https://github.com/mapsforge/vtm/issues/387)
Expand Down
3 changes: 3 additions & 0 deletions vtm-android-example/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@
<activity
android:name=".POTTextureActivity"
android:configChanges="keyboardHidden|orientation|screenSize" />
<activity
android:name=".ReverseGeocodeActivity"
android:configChanges="keyboardHidden|orientation|screenSize" />
<activity
android:name=".RotateMarkerOverlayActivity"
android:configChanges="keyboardHidden|orientation|screenSize" />
Expand Down
1 change: 1 addition & 0 deletions vtm-android-example/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@
<string name="style_1">Show nature</string>
<string name="style_2">Hide nature</string>
<string name="menu_gridlayer">Grid</string>
<string name="dialog_reverse_geocoding_title">Reverse Geocoding</string>

</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public class MapsforgeMapActivity extends MapActivity {
private TileGridLayer mGridLayer;
private DefaultMapScaleBar mMapScaleBar;
private Menu mMenu;
MapFileTileSource mTileSource;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -152,12 +153,12 @@ protected void onActivityResult(int requestCode, int resultCode, Intent intent)
return;
}

MapFileTileSource tileSource = new MapFileTileSource();
tileSource.setPreferredLanguage("en");
mTileSource = new MapFileTileSource();
mTileSource.setPreferredLanguage("en");
String file = intent.getStringExtra(FilePicker.SELECTED_FILE);
if (tileSource.setMapFile(file)) {
if (mTileSource.setMapFile(file)) {

VectorTileLayer l = mMap.setBaseMap(tileSource);
VectorTileLayer l = mMap.setBaseMap(mTileSource);
loadTheme(null);

mMap.layers().add(new BuildingLayer(mMap, l));
Expand All @@ -175,7 +176,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent intent)
renderer.setOffset(5 * getResources().getDisplayMetrics().density, 0);
mMap.layers().add(mapScaleBarLayer);

MapInfo info = tileSource.getMapInfo();
MapInfo info = mTileSource.getMapInfo();
MapPosition pos = new MapPosition();
pos.setByBoundingBox(info.boundingBox, Tile.SIZE * 4, Tile.SIZE * 4);
mMap.setMapPosition(pos);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,11 @@ protected void onActivityResult(int requestCode, int resultCode, Intent intent)
super.onActivityResult(requestCode, resultCode, intent);

if (requestCode == SELECT_MAP_FILE) {
startActivityForResult(new Intent(this, PoiFilePicker.class),
SELECT_POI_FILE);
if (mTileSource != null)
startActivityForResult(new Intent(this, PoiFilePicker.class),
SELECT_POI_FILE);
else
finish();
} else if (requestCode == SELECT_POI_FILE) {
if (resultCode != RESULT_OK || intent == null || intent.getStringExtra(FilePicker.SELECTED_FILE) == null) {
finish();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/*
* Copyright 2017 devemux86
*
* This program is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.oscim.android.test;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;

import org.oscim.backend.CanvasAdapter;
import org.oscim.core.GeoPoint;
import org.oscim.core.GeometryBuffer;
import org.oscim.core.MercatorProjection;
import org.oscim.core.Point;
import org.oscim.core.Tag;
import org.oscim.core.Tile;
import org.oscim.event.Gesture;
import org.oscim.event.GestureListener;
import org.oscim.event.MotionEvent;
import org.oscim.layers.Layer;
import org.oscim.layers.TileGridLayer;
import org.oscim.map.Map;
import org.oscim.tiling.source.mapfile.MapDatabase;
import org.oscim.tiling.source.mapfile.MapReadResult;
import org.oscim.tiling.source.mapfile.PointOfInterest;
import org.oscim.tiling.source.mapfile.Way;
import org.oscim.utils.GeoPointUtils;

import java.util.List;

/**
* Reverse Geocoding with long press.
* <p/>
* - POI in specified radius.<br/>
* - Ways containing touch point.
*/
public class ReverseGeocodeActivity extends MapsforgeMapActivity {

private static final int TOUCH_RADIUS = 32 / 2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Map events receiver
mMap.layers().add(new MapEventsReceiver(mMap));
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
return false;
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);

if (requestCode == SELECT_MAP_FILE) {
// For debug
TileGridLayer gridLayer = new TileGridLayer(mMap, getResources().getDisplayMetrics().density);
mMap.layers().add(gridLayer);
}
}

private class MapEventsReceiver extends Layer implements GestureListener {

MapEventsReceiver(Map map) {
super(map);
}

@Override
public boolean onGesture(Gesture g, MotionEvent e) {
if (g instanceof Gesture.LongPress) {
GeoPoint p = mMap.viewport().fromScreenPoint(e.getX(), e.getY());

// Read all labeled POI and ways for the area covered by the tiles under touch
float touchRadius = TOUCH_RADIUS * CanvasAdapter.dpi / CanvasAdapter.DEFAULT_DPI;
long mapSize = MercatorProjection.getMapSize((byte) mMap.getMapPosition().getZoomLevel());
double pixelX = MercatorProjection.longitudeToPixelX(p.getLongitude(), mapSize);
double pixelY = MercatorProjection.latitudeToPixelY(p.getLatitude(), mapSize);
int tileXMin = MercatorProjection.pixelXToTileX(pixelX - touchRadius, (byte) mMap.getMapPosition().getZoomLevel());
int tileXMax = MercatorProjection.pixelXToTileX(pixelX + touchRadius, (byte) mMap.getMapPosition().getZoomLevel());
int tileYMin = MercatorProjection.pixelYToTileY(pixelY - touchRadius, (byte) mMap.getMapPosition().getZoomLevel());
int tileYMax = MercatorProjection.pixelYToTileY(pixelY + touchRadius, (byte) mMap.getMapPosition().getZoomLevel());
Tile upperLeft = new Tile(tileXMin, tileYMin, (byte) mMap.getMapPosition().getZoomLevel());
Tile lowerRight = new Tile(tileXMax, tileYMax, (byte) mMap.getMapPosition().getZoomLevel());
MapReadResult mapReadResult = ((MapDatabase) mTileSource.getDataSource()).readLabels(upperLeft, lowerRight);

StringBuilder sb = new StringBuilder();

// Filter POI
sb.append("*** POI ***");
for (PointOfInterest pointOfInterest : mapReadResult.pointOfInterests) {
Point layerXY = new Point();
mMap.viewport().toScreenPoint(pointOfInterest.position, false, layerXY);
Point tapXY = new Point(e.getX(), e.getY());
if (layerXY.distance(tapXY) > touchRadius) {
continue;
}
sb.append("\n");
List<Tag> tags = pointOfInterest.tags;
for (Tag tag : tags) {
sb.append("\n").append(tag.key).append("=").append(tag.value);
}
}

// Filter ways
sb.append("\n\n").append("*** WAYS ***");
for (Way way : mapReadResult.ways) {
if (way.geometryType != GeometryBuffer.GeometryType.POLY
|| !GeoPointUtils.contains(way.geoPoints[0], p)) {
continue;
}
sb.append("\n");
List<Tag> tags = way.tags;
for (Tag tag : tags) {
sb.append("\n").append(tag.key).append("=").append(tag.value);
}
}

AlertDialog.Builder builder = new AlertDialog.Builder(ReverseGeocodeActivity.this);
builder.setIcon(android.R.drawable.ic_menu_search);
builder.setTitle(R.string.dialog_reverse_geocoding_title);
builder.setMessage(sb);
builder.setPositiveButton(R.string.ok, null);
builder.show();

return true;
}
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ public void onClick(View v) {
linearLayout.addView(createButton(MultiMapActivity.class));

linearLayout.addView(createLabel("Experiments"));
linearLayout.addView(createButton(ReverseGeocodeActivity.class));
linearLayout.addView(createButton(MapPositionActivity.class));
linearLayout.addView(createButton(S3DBMapActivity.class));
linearLayout.addView(createButton(ThemeStylerActivity.class));
Expand Down
5 changes: 5 additions & 0 deletions vtm/src/org/oscim/layers/tile/MapTile.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright 2012, 2013 Hannes Janetzek
* Copyright 2017 devemux86
*
* This file is part of the OpenScienceMap project (http://www.opensciencemap.org).
*
Expand Down Expand Up @@ -158,6 +159,10 @@ public TileData next() {
}
}

public MapTile(int tileX, int tileY, int zoomLevel) {
this(null, tileX, tileY, zoomLevel);
}

public MapTile(TileNode node, int tileX, int tileY, int zoomLevel) {
super(tileX, tileY, (byte) zoomLevel);
this.x = (double) tileX / (1 << zoomLevel);
Expand Down
Loading

0 comments on commit 5c2b404

Please sign in to comment.