gwt: geojson tilesource

This commit is contained in:
Hannes Janetzek 2014-03-30 04:16:29 +02:00
parent 5fd05d5d01
commit de27227ed7
16 changed files with 734 additions and 1 deletions

View File

@ -72,7 +72,7 @@ public class GeoJsonTileDecoder implements ITileDecoder {
private double mTileY, mTileX, mTileScale;
GeoJsonTileDecoder(GeoJsonTileSource tileSource) {
public GeoJsonTileDecoder(GeoJsonTileSource tileSource) {
mTileSource = tileSource;
mTagMap = new LinkedHashMap<String, Object>();
mJsonFactory = new JsonFactory();

View File

@ -24,6 +24,7 @@ sourceSets {
dependencies {
providedCompile project(':vtm-web')
providedCompile project(':vtm-extras')
providedCompile "com.badlogicgames.gdx:gdx:$gdxVersion:sources"
providedCompile "com.badlogicgames.gdx:gdx-backend-gwt:$gdxVersion:sources"
providedCompile "com.badlogicgames.gdx:gdx-backend-gwt:$gdxVersion"
@ -36,6 +37,7 @@ dependencies {
evaluationDependsOn(':vtm')
evaluationDependsOn(':vtm-themes')
evaluationDependsOn(':vtm-gdx')
//evaluationDependsOn(':vtm-extras')
evaluationDependsOn(':vtm-web')
gwt {
@ -56,6 +58,7 @@ gwt {
src += files(project(':vtm-themes').sourceSets.main.allJava.srcDirs)
src += files(project(':vtm-themes').sourceSets.main.resources.srcDirs)
src += files(project(':vtm-gdx').sourceSets.main.allJava.srcDirs)
//src += files(project(':vtm-extras').sourceSets.main.allJava.srcDirs)
src += files(project(':vtm-web').sourceSets.main.allJava.srcDirs)
}

View File

@ -25,6 +25,7 @@ sourceSets {
dependencies {
compile project(':vtm-gdx')
compile project(':vtm-extras')
compile "com.badlogicgames.gdx:gdx:$gdxVersion:sources"
compile "com.badlogicgames.gdx:gdx-backend-gwt:$gdxVersion:sources"
compile "com.badlogicgames.gdx:gdx-backend-gwt:$gdxVersion"
@ -36,6 +37,7 @@ dependencies {
evaluationDependsOn(':vtm')
evaluationDependsOn(':vtm-themes')
evaluationDependsOn(':vtm-gdx')
evaluationDependsOn(':vtm-extras')
gwt {
gwtVersion='2.6.0'

View File

@ -0,0 +1,63 @@
/*
* Copyright 2014 Hannes Janetzek
*
* This file is part of the OpenScienceMap project (http://www.opensciencemap.org).
*
* 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.tiling.source.geojson;
import java.util.Map;
import org.oscim.core.MapElement;
import org.oscim.core.Tag;
import org.oscim.tiling.ITileDataSource;
import org.oscim.tiling.source.JsonTileDataSource;
import org.oscim.tiling.source.UrlTileSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public abstract class GeoJsonTileSource extends UrlTileSource {
static final Logger log = LoggerFactory.getLogger(GeoJsonTileSource.class);
public GeoJsonTileSource(String url) {
super(url);
setExtension(".json");
}
@Override
public ITileDataSource getDataSource() {
return new JsonTileDataSource(this);
}
public Tag getFeatureTag() {
return null;
}
/** allow overriding tag handling */
public abstract void decodeTags(MapElement mapElement, Map<String, Object> properties);
public Tag rewriteTag(String key, Object value) {
if (value == null)
return null;
String val = (value instanceof String) ? (String) value : String.valueOf(value);
return new Tag(key, val);
}
/** modify mapElement before process() */
public void postGeomHook(MapElement mapElement) {
}
}

View File

@ -0,0 +1,126 @@
/*
* Copyright 2014 Hannes Janetzek
*
* This file is part of the OpenScienceMap project (http://www.opensciencemap.org).
*
* 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.tiling.source;
import static org.oscim.tiling.ITileDataSink.QueryResult.FAILED;
import static org.oscim.tiling.ITileDataSink.QueryResult.SUCCESS;
import java.io.InputStream;
import org.oscim.layers.tile.MapTile;
import org.oscim.layers.tile.MapTile.State;
import org.oscim.tiling.ITileDataSink;
import org.oscim.tiling.ITileDataSource;
import org.oscim.tiling.source.geojson.GeoJsonTileDecoder;
import org.oscim.tiling.source.geojson.GeoJsonTileSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.jsonp.client.JsonpRequest;
import com.google.gwt.jsonp.client.JsonpRequestBuilder;
import com.google.gwt.user.client.rpc.AsyncCallback;
public class JsonTileDataSource implements ITileDataSource {
static final Logger log = LoggerFactory.getLogger(JsonTileDataSource.class);
protected final GeoJsonTileDecoder mTileDecoder;
protected final UrlTileSource mTileSource;
private final byte[] mRequestBuffer = new byte[1024];
public JsonTileDataSource(GeoJsonTileSource tileSource) {
mTileSource = tileSource;
mTileDecoder = new GeoJsonTileDecoder(tileSource);
}
UrlTileSource getTileSource() {
return mTileSource;
}
private ITileDataSink mSink;
private MapTile mTile;
@Override
public void query(MapTile tile, ITileDataSink sink) {
mTile = tile;
mSink = sink;
try {
int pos = mTileSource.formatTilePath(tile, mRequestBuffer, 0);
String url = mTileSource.getUrl()
+ (new String(mRequestBuffer, 0, pos));
doGet(url);
} catch (Exception e) {
e.printStackTrace();
sink.completed(FAILED);
}
}
public void process(InputStream is) {
}
boolean mFinished;
@Override
public void destroy() {
mFinished = true;
}
JsonpRequest<JavaScriptObject> mRequestHandle;
void doGet(final String url) {
JsonpRequestBuilder builder = new JsonpRequestBuilder();
//builder.setCallbackParam("json_callback");
mRequestHandle = builder.requestObject(url, new AsyncCallback<JavaScriptObject>() {
public void onFailure(Throwable caught) {
mSink.completed(FAILED);
log.debug("fail! {} {}", mRequestHandle, caught.getMessage());
//mRequestHandle.cancel();
}
public void onSuccess(JavaScriptObject jso) {
if (mTile.state(State.NONE)) {
log.debug("tile cleared {}", url);
mSink.completed(FAILED);
return;
}
if (jso == null) {
log.debug("Couldn't retrieve JSON for {}", url);
mSink.completed(FAILED);
return;
}
try {
if (mTileDecoder.decode(mTile, mSink, jso)) {
mSink.completed(SUCCESS);
return;
}
} catch (Exception e) {
log.debug("Couldn't retrieve JSON for {} {}" + url, e);
// FIXME need to check where it might be thrown
mSink.completed(FAILED);
}
}
});
}
}

View File

@ -0,0 +1,52 @@
/*
* Copyright 2014 Hannes Janetzek
*
* This file is part of the OpenScienceMap project (http://www.opensciencemap.org).
*
* 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.tiling.source.geojson;
import java.util.HashMap;
import java.util.Map;
public class Feature extends GeoJsonObject {
protected Feature() {
}
public final native Geometry<?> getGeometry() /*-{
return this.geometry;
}-*/;
public final native String getId() /*-{
return this.id;
}-*/;
public final native void setId(String id) /*-{
this.id = id;
}-*/;
public final Map<String, Object> getProperties(HashMap<String, Object> map) {
map.clear();
fromJavascriptObject(map);
return map;
}
public final native void fromJavascriptObject(HashMap<String, Object> s) /*-{
for(var key in this.properties) {
s.@java.util.HashMap::put(Ljava/lang/Object;Ljava/lang/Object;)(key, Object(this.properties[key]));
}
}-*/;
}

View File

@ -0,0 +1,38 @@
/*
* Copyright 2014 Hannes Janetzek
*
* This file is part of the OpenScienceMap project (http://www.opensciencemap.org).
*
* 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.tiling.source.geojson;
import java.util.Collection;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsArray;
public class FeatureCollection extends JavaScriptObject {
protected FeatureCollection() {
}
public final Collection<Feature> getFeatures() {
return new JsArrayCollection<Feature>(getFeaturesInternal());
}
public final native JsArray<Feature> getFeaturesInternal()/*-{
return this.features;
}-*/;
}

View File

@ -0,0 +1,34 @@
/*
* Copyright 2014 Hannes Janetzek
*
* This file is part of the OpenScienceMap project (http://www.opensciencemap.org).
*
* 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.tiling.source.geojson;
import com.google.gwt.core.client.JavaScriptObject;
public abstract class GeoJsonObject extends JavaScriptObject {
protected GeoJsonObject() {
}
public final native double[] getBbox()/*-{
return bbox;
}-*/;
public final native void setBbox(double[] bbox) /*-{
this.bbox = bbox;
}-*/;
}

View File

@ -0,0 +1,139 @@
/*
* Copyright 2014 Hannes Janetzek
*
* This file is part of the OpenScienceMap project (http://www.opensciencemap.org).
*
* 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.tiling.source.geojson;
import static org.oscim.core.MercatorProjection.latitudeToY;
import static org.oscim.core.MercatorProjection.longitudeToX;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedHashMap;
import org.oscim.core.GeometryBuffer.GeometryType;
import org.oscim.core.MapElement;
import org.oscim.core.Tile;
import org.oscim.tiling.ITileDataSink;
import org.oscim.tiling.source.ITileDecoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.gwt.core.client.JavaScriptObject;
public class GeoJsonTileDecoder implements ITileDecoder {
static final Logger log = LoggerFactory.getLogger(GeoJsonTileDecoder.class);
private final MapElement mapElement;
private final GeoJsonTileSource mTileSource;
private ITileDataSink mTileDataSink;
public GeoJsonTileDecoder(GeoJsonTileSource tileSource) {
mTileSource = tileSource;
mapElement = new MapElement();
mapElement.layer = 5;
}
final static LinkedHashMap<String, Object> mProperties = new LinkedHashMap<String, Object>(10);
double mTileY, mTileX, mTileScale;
public boolean decode(Tile tile, ITileDataSink sink, JavaScriptObject jso) {
mTileDataSink = sink;
mTileScale = 1 << tile.zoomLevel;
mTileX = tile.tileX / mTileScale;
mTileY = tile.tileY / mTileScale;
mTileScale *= Tile.SIZE;
FeatureCollection c = (FeatureCollection) jso;
for (Feature f : c.getFeatures()) {
mapElement.clear();
mapElement.tags.clear();
/* add tag information */
mTileSource.decodeTags(mapElement, f.getProperties(mProperties));
if (mapElement.tags.numTags == 0)
continue;
/* add geometry information */
decodeGeometry(f.getGeometry());
if (mapElement.type == GeometryType.NONE)
continue;
mTileDataSink.process(mapElement);
}
return true;
}
private void decodeGeometry(Geometry<?> geometry) {
String type = geometry.type();
if ("Polygon".equals(type)) {
Polygon p = (Polygon) geometry.getCoordinates();
decodePolygon(p);
} else if ("MultiPolygon".equals(type)) {
MultiPolygon mp = (MultiPolygon) geometry.getCoordinates();
for (int k = 0, l = mp.getNumGeometries(); k < l; k++)
decodePolygon(mp.getGeometryN(k));
} else if ("LineString".equals(type)) {
LineString ls = (LineString) geometry.getCoordinates();
decodeLineString(ls);
} else if ("MultiLineString".equals(type)) {
MultiLineString ml = (MultiLineString) geometry.getCoordinates();
for (int k = 0, n = ml.getNumGeometries(); k < n; k++)
decodeLineString(ml.getGeometryN(k));
}
}
private void decodeLineString(LineString l) {
mapElement.startLine();
for (int j = 0, m = l.length(); j < m; j++) {
decodePoint(l.get(j));
}
}
private void decodePolygon(Polygon p) {
for (int i = 0, n = p.getNumRings(); i < n; i++) {
if (i > 0)
mapElement.startHole();
else
mapElement.startPolygon();
LineString ls = p.getRing(i);
for (int j = 0, m = ls.length() - 1; j < m; j++)
decodePoint(ls.get(j));
}
}
private void decodePoint(LngLat point) {
float x = (float) ((longitudeToX(point.getLongitude()) - mTileX) * mTileScale);
float y = (float) ((latitudeToY(point.getLatitude()) - mTileY) * mTileScale);
mapElement.addPoint(x, y);
}
@Override
public boolean decode(Tile tile, ITileDataSink sink, InputStream is) throws IOException {
return false;
}
}

View File

@ -0,0 +1,35 @@
/*
* Copyright 2014 Hannes Janetzek
*
* This file is part of the OpenScienceMap project (http://www.opensciencemap.org).
*
* 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.tiling.source.geojson;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsArray;
public abstract class Geometry<T extends JavaScriptObject> extends JsArray<T> {
protected Geometry() {
}
public final native String type()/*-{
return this.type
}-*/;
public final native JsArray<T> getCoordinates() /*-{
return this.coordinates;
}-*/;
}

View File

@ -0,0 +1,100 @@
package org.oscim.tiling.source.geojson;
import java.util.AbstractCollection;
import java.util.Iterator;
import com.google.gwt.core.client.JavaScriptObject;
/**
* a Java Friendly way of working with Js Arrays using the Java.util.Collection
* API
* https://groups.google.com/forum/#!topic/google-web-toolkit/_8X9WPL6qwM
*
* @author sg
*
* @param <T>
*/
public class JsArrayCollection<T> extends AbstractCollection<T> {
private JsArr<T> _data;
/**
* creates an empty array
*/
public JsArrayCollection() {
_data = JsArr.create().cast();
}
/**
* creates JsArrayCollection wrapping an existing js array
*/
public JsArrayCollection(JavaScriptObject data) {
this._data = data.cast();
}
public static <T> JsArrayCollection<T> create(JavaScriptObject data) {
return new JsArrayCollection<T>(data);
}
@Override
public Iterator<T> iterator() {
return new JsArrayIterator<T>(this);
}
@Override
public int size() {
return _data.size();
}
public static class JsArrayIterator<T> implements Iterator<T> {
private JsArrayCollection<T> arr;
int currentIndex;
public JsArrayIterator(JsArrayCollection<T> arr) {
this.arr = arr;
currentIndex = 0;
}
@Override
public boolean hasNext() {
// System.out.println(currentIndex+" - "+arr.size());
return currentIndex < arr.size();
}
@Override
public T next() {
currentIndex++;
return arr._data.get(currentIndex - 1);
}
@Override
public void remove() {
arr._data.slice(currentIndex - 1, currentIndex);
}
}
/** untyped array */
private static class JsArr<T> extends JavaScriptObject {
protected JsArr() {
}
public native final JsArr<T> slice(int start, int end)/*-{
return this.slice(start, end);
}-*/;
public static final native <T> JsArr<T> create() /*-{
return [];
}-*/;
public final native int size() /*-{
return this.length;
}-*/;
public final native T get(int i) /*-{
return this[i];
}-*/;
}
}

View File

@ -0,0 +1,23 @@
/*
* Copyright 2014 Hannes Janetzek
*
* This file is part of the OpenScienceMap project (http://www.opensciencemap.org).
*
* 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.tiling.source.geojson;
public class LineString extends Geometry<LngLat> {
protected LineString() {
}
}

View File

@ -0,0 +1,18 @@
package org.oscim.tiling.source.geojson;
import com.google.gwt.core.client.JavaScriptObject;
public class LngLat extends JavaScriptObject {
protected LngLat() {
}
public final native double getLongitude() /*-{
return this[0];
}-*/;
public final native double getLatitude() /*-{
return this[1];
}-*/;
}

View File

@ -0,0 +1,32 @@
/*
* Copyright 2014 Hannes Janetzek
*
* This file is part of the OpenScienceMap project (http://www.opensciencemap.org).
*
* 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.tiling.source.geojson;
public class MultiLineString extends Geometry<LineString> {
protected MultiLineString() {
}
public final native LineString getGeometryN(int i) /*-{
return this[i];
}-*/;
public final native int getNumGeometries() /*-{
return this.length;
}-*/;
}

View File

@ -0,0 +1,32 @@
/*
* Copyright 2014 Hannes Janetzek
*
* This file is part of the OpenScienceMap project (http://www.opensciencemap.org).
*
* 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.tiling.source.geojson;
public class MultiPolygon extends Geometry<Polygon> {
protected MultiPolygon() {
}
public final native Polygon getGeometryN(int i) /*-{
return this[i];
}-*/;
public final native int getNumGeometries() /*-{
return this.length;
}-*/;
}

View File

@ -0,0 +1,36 @@
/*
* Copyright 2014 Hannes Janetzek
*
* This file is part of the OpenScienceMap project (http://www.opensciencemap.org).
*
* 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.tiling.source.geojson;
public class Polygon extends Geometry<LineString> {
protected Polygon() {
}
public final native LineString getExteriorRing()/*-{
return this[0];
}-*/;
public final native LineString getRing(int i) /*-{
return this[i];
}-*/;
public final native int getNumRings() /*-{
return this.length;
}-*/;
}