Created
October 19, 2012 14:24
-
-
Save nutiteq/3918480 to your computer and use it in GitHub Desktop.
WfsTask.java - geometry layer sample task
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.nutiteq.app.layers; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.io.StringReader; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
import java.util.LinkedList; | |
import java.util.List; | |
import java.util.Vector; | |
import java.util.concurrent.locks.ReentrantLock; | |
import javax.xml.parsers.ParserConfigurationException; | |
import org.xml.sax.SAXException; | |
import org.xmlpull.v1.XmlPullParser; | |
import org.xmlpull.v1.XmlPullParserException; | |
import org.xmlpull.v1.XmlPullParserFactory; | |
import com.nutiteq.components.ImmutableMapPos; | |
import com.nutiteq.components.MapPos; | |
import com.nutiteq.geometry.Geometry; | |
import com.nutiteq.geometry.Line; | |
import com.nutiteq.geometry.Polygon; | |
import com.nutiteq.log.Log; | |
import com.nutiteq.projections.Projection; | |
import com.nutiteq.style.LineStyle; | |
import com.nutiteq.style.PointStyle; | |
import com.nutiteq.style.PolygonStyle; | |
import com.nutiteq.style.StyleSet; | |
import com.nutiteq.tasks.Task; | |
import com.nutiteq.ui.DefaultLabel; | |
import com.nutiteq.vectorlayers.GeometryLayer; | |
import com.vividsolutions.jts.geom.Coordinate; | |
import com.vividsolutions.jts.geom.Envelope; | |
import com.vividsolutions.jts.geom.GeometryCollection; | |
import com.vividsolutions.jts.geom.LineString; | |
import com.vividsolutions.jts.geom.MultiLineString; | |
import com.vividsolutions.jts.geom.MultiPolygon; | |
import com.vividsolutions.jts.geom.Point; | |
import com.vividsolutions.jts.io.gml2.GMLReader; | |
// NB! This parses currently only Point elements, but it would be quite easy to extend it to lines and polygons | |
public class WfsTask implements Task { | |
private final GeometryLayer geoLayer; | |
private final ReentrantLock modifyLock; | |
private final Envelope envelope; | |
private final int zoom; | |
private Projection projection; | |
private StyleSet<PointStyle> pointStyleSet; | |
private StyleSet<LineStyle> lineStyleSet; | |
private StyleSet<PolygonStyle> polygonStyle; | |
private String url; | |
private Projection dataProjection; | |
public WfsTask(Projection projection, String url, Projection dataProjection, GeometryLayer geoLayer, | |
ReentrantLock modifyLock, Envelope envelope, int zoom, StyleSet<PointStyle> pointStyleSet, | |
StyleSet<LineStyle> lineStyleSet, | |
StyleSet<PolygonStyle> polygonStyle) { | |
this.geoLayer = geoLayer; | |
this.modifyLock = modifyLock; | |
this.envelope = envelope; | |
this.zoom = zoom; | |
this.projection = projection; | |
this.dataProjection = dataProjection; | |
this.url = url; | |
this.pointStyleSet = pointStyleSet; | |
this.lineStyleSet = lineStyleSet; | |
this.polygonStyle = polygonStyle; | |
} | |
@Override | |
public void run() { | |
modifyLock.lock(); | |
List<Geometry> newVisibleElementsList = downloadGeometries(); | |
modifyLock.unlock(); | |
Log.debug("adding elements N="+newVisibleElementsList.size()); | |
geoLayer.setVisibleElementsList(newVisibleElementsList); | |
} | |
private List<Geometry> downloadGeometries() { | |
Log.debug("download geometries for envelope "+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)); | |
// convert internal projection->wgs84->dataProjection | |
String bbox = ""+ | |
dataProjection.fromWgs84( | |
projection.toWgs84(bottomLeft.x,bottomLeft.y).x, | |
projection.toWgs84(bottomLeft.x,bottomLeft.y).y).x+ | |
","+ | |
dataProjection.fromWgs84(projection.toWgs84(bottomLeft.x,bottomLeft.y).x,projection.toWgs84(bottomLeft.x,bottomLeft.y).y).y+ | |
","+ | |
dataProjection.fromWgs84(projection.toWgs84(topRight.x,topRight.y).x,projection.toWgs84(topRight.x,topRight.y).y).x+ | |
","+ | |
dataProjection.fromWgs84(projection.toWgs84(topRight.x,topRight.y).x,projection.toWgs84(topRight.x,topRight.y).y).y; | |
List<Geometry> newVisibleElementsList = new LinkedList<Geometry>(); | |
HttpURLConnection con = null; | |
String path = url+"&BBOX="+bbox; | |
URL url; | |
InputStream is = null; | |
try { | |
url = new URL(path); | |
con = (HttpURLConnection) url.openConnection(); | |
con.setReadTimeout(10000 /* milliseconds */); | |
con.setConnectTimeout(15000 /* milliseconds */); | |
con.setRequestMethod("GET"); | |
con.setDoInput(true); | |
// Start the GET query | |
con.connect(); | |
Log.debug("connected "+path); | |
is = con.getInputStream(); | |
BufferedReader reader = new BufferedReader( | |
new InputStreamReader(is)); | |
String txt = null; | |
String year = null; | |
String cover = null; | |
float x = 0; | |
float y = 0; | |
XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); | |
factory.setNamespaceAware(false); | |
XmlPullParser xpp = factory.newPullParser(); | |
xpp.setInput(reader); | |
int eventType = xpp.getEventType(); | |
while (eventType != XmlPullParser.END_DOCUMENT) { | |
switch(eventType){ | |
case XmlPullParser.START_DOCUMENT: | |
Log.debug("Start XML"); | |
break; | |
case XmlPullParser.END_DOCUMENT: | |
Log.debug("End XML"); | |
break; | |
case XmlPullParser.START_TAG: | |
// Log.debug("Start tag "+xpp.getName()); | |
// get tag name | |
String tagName = xpp.getName(); | |
if(tagName.equals("gml:coordinates")){ | |
String coords = xpp.nextText(); | |
Log.debug("coordinates in tag: "+coords); | |
String[] cp = coords.split(","); | |
x = Float.valueOf(cp[0]); | |
y = Float.valueOf(cp[1]); | |
} | |
break; | |
case XmlPullParser.END_TAG: | |
String name = xpp.getName(); | |
Log.debug("End tag "+name); | |
// some schema-specific tags for labels | |
if(name.equals("tvesi:YEAR")){ | |
year = txt; | |
} | |
if(name.equals("tvesi:COVERTYPE")){ | |
cover = txt; | |
} | |
if(name.equals("gml:featureMember")){ | |
DefaultLabel label = new DefaultLabel("COVERTYPE:"+cover,"YEAR:"+year); | |
com.nutiteq.geometry.Point point = new com.nutiteq.geometry.Point(new ImmutableMapPos((float) x, (float) y), | |
label, pointStyleSet, null); | |
point.calculateInternalPos(dataProjection); | |
point.setActiveStyle(zoom); | |
newVisibleElementsList.add(point); | |
} | |
break; | |
case XmlPullParser.TEXT: | |
txt = xpp.getText(); | |
break; | |
} | |
eventType = xpp.next(); | |
} | |
} catch (IOException ex) { | |
Log.error(ex.getMessage()); | |
} catch (XmlPullParserException e) { | |
Log.error(e.getMessage()); | |
e.printStackTrace(); | |
} finally { | |
try { | |
if(is != null) | |
is.close(); | |
} catch (IOException e) { | |
Log.error(e.getMessage()); | |
} | |
} | |
return newVisibleElementsList; | |
} | |
@Override | |
public boolean isCancelable() { | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment