Skip to content

Instantly share code, notes, and snippets.

@nutiteq
Created October 6, 2012 18:10
Show Gist options
  • Save nutiteq/3845673 to your computer and use it in GitHub Desktop.
Save nutiteq/3845673 to your computer and use it in GitHub Desktop.
Wikipedia Layer for Nutiteq 3D maps SDK - task to download data
package com.nutiteq.app.layers;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.ReentrantLock;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.nutiteq.components.ImmutableMapPos;
import com.nutiteq.components.MapPos;
import com.nutiteq.geometry.Marker;
import com.nutiteq.log.Log;
import com.nutiteq.projections.Projection;
import com.nutiteq.style.MarkerStyle;
import com.nutiteq.style.StyleSet;
import com.nutiteq.tasks.Task;
import com.nutiteq.ui.DefaultLabel;
import com.nutiteq.utils.IOUtils;
import com.vividsolutions.jts.geom.Envelope;
public class WikipediaTask implements Task {
private final WikipediaLayer markerLayer;
private final ReentrantLock modifyLock;
private final Envelope envelope;
private final int zoom;
private StyleSet<MarkerStyle> style;
private Projection projection;
public WikipediaTask(Projection projection, WikipediaLayer markerLayer,
ReentrantLock modifyLock, Envelope envelope, int zoom, StyleSet<MarkerStyle> style) {
this.markerLayer = markerLayer;
this.modifyLock = modifyLock;
this.envelope = envelope;
this.zoom = zoom;
this.style = style;
this.projection = projection;
}
@Override
public void run() {
modifyLock.lock();
List<Marker> newVisibleElementsList = downloadMarkers(envelope);
modifyLock.unlock();
Log.debug("adding wikipedia elements N="+newVisibleElementsList.size());
markerLayer.setVisibleElementsList(newVisibleElementsList);
}
private List<Marker> downloadMarkers(Envelope envelope) {
Log.debug("download wiki "+envelope+" zoom "+zoom);
MapPos bottomLeft = projection.fromInternal((float) envelope.getMinX(), (float) envelope.getMinY());
MapPos topRight = projection.fromInternal((float) envelope.getMaxX(), (float) envelope.getMaxY());
Log.debug("bottomLeft "+projection.toWgs84(bottomLeft.x,bottomLeft.y)+" topRight "+projection.toWgs84(topRight.x,topRight.y));
String bbox = "west="+projection.toWgs84(bottomLeft.x,bottomLeft.y).x+"&south="+projection.toWgs84(bottomLeft.x,bottomLeft.y).y+"&east="+projection.toWgs84(topRight.x,topRight.y).x+"&north="+projection.toWgs84(topRight.x,topRight.y).y;
List<Marker> pois = new ArrayList<Marker>();
// Working sample: http://api.geonames.org/wikipediaBoundingBoxJSON?north=north=44.1&south=-9.9&east=-22.4&west=55.2&username=demo
String path = "http://api.geonames.org/wikipediaBoundingBoxJSON?"+bbox+"&maxRows=30&username=nutiteq";
Log.debug("wiki "+path);
try {
URL url = new URL(path);
InputStream urlInputStream = url.openConnection().getInputStream();
// parse data from JSON
JSONObject json = new JSONObject(new String(IOUtils.readFully(urlInputStream)));
JSONArray geonames = json.getJSONArray("geonames");
for(int i = 0; i < geonames.length(); i++){
JSONObject geoname = geonames.getJSONObject(i);
float lon = Float.parseFloat(geoname.getString("lng"));
float lat = Float.parseFloat(geoname.getString("lat"));
String title = geoname.getString("title");
String feature = "";
String countryCode = "";
try{
// optional fields
feature = geoname.getString("feature");
countryCode = " in "+geoname.getString("countryCode");
} catch (JSONException e) {
Log.debug(e.getMessage());
}
ImmutableMapPos location = projection.fromWgs84(lon, lat);
Marker poi = new Marker(location, new DefaultLabel(
title, feature+countryCode), this.style, geoname);
poi.calculateInternalPos(projection);
poi.setActiveStyle(zoom);
pois.add(poi);
// Log.debug("added wiki marker "+title+ " "+lat+" "+lon);
}
Log.debug("added "+geonames.length()+" wikipedia markers");
} catch (IOException ex) {
Log.error(ex.getMessage());
} catch (JSONException e) {
Log.error(e.getMessage());
e.printStackTrace();
} finally {
}
return pois;
}
@Override
public boolean isCancelable() {
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment