/* * GML2Reader.java * * Created on 16 September 2001, 16:50 */ package uk.ac.leeds.ccg.gml; import org.w3c.dom.Document; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.apache.xerces.parsers.DOMParser; import com.galdosinc.xml.XMLException; import com.galdosinc.gml.util.*; import com.galdosinc.gml.infoset.*; import com.galdosinc.gml.dom.GMLDocument; import uk.ac.leeds.ccg.geotools.*; import java.net.*; import java.io.*; /** * * @author James Macgill * @version */ public class GML2Reader implements uk.ac.leeds.ccg.geotools.FeatureReader { GMLDocument gmlDoc; private final static boolean DEBUG=true; /** * Build a list of features from GML. * There is a security * constraint in java applets which prevents an applet from accessing a * URL different to the server where the applet came from. So if you want * to access a Web Feature Server from somewhere on the web, you need to set * up a local proxy. A local proxy sits on the server where the applet * came from, it recieves calls from the applet, forwards them onto the * Web Feature Server, and then funnels the responses back to the applet. *
If you are not using an applet, or if the URL of the GML is on the * same server as the applet, then you can set proxy to null. * @param url URL of the GML features * @param proxy URL of the local proxy server. */ public GML2Reader(URL url,URL proxy) { InputSource inputSource; try{ DOMParser parser = new DOMParser(); System.out.println("Parsing"); if(DEBUG)System.out.println("GML2Reader: url="+url); if (proxy==null){ inputSource = new InputSource(url.openStream()); parser.parse(inputSource); }else{ URL url2=new URL( proxy.toString() +"?ProxyURL=" +URLEncoder.encode(url.toString())); inputSource = new InputSource(url2.openStream()); if(DEBUG)System.out.println("GML2Reader: url2="+url2); } Document document = parser.getDocument(); gmlDoc = new GMLDocument(document); } catch(Exception e){ System.err.println("GML inialization failed because of "+e); e.printStackTrace(); } } public GML2Reader(URL url) { this(url,null); } public Layer getLayer() { MixedLayer layer=new MixedLayer(); GMLConstructIterator iterator = gmlDoc.getGMLConstructIterator(); GMLConstruct firstGMLConstruct = iterator.nextGMLConstruct(); this.recursiveAddGeometry(layer,firstGMLConstruct); return layer; } public void recursiveAddGeometry(MixedLayer layer,GMLConstruct gmlConstruct) { GMLConstructIterator iterator = gmlConstruct.getGMLConstructIterator(); while (iterator.hasNext()) { GMLConstruct nextGMLConstruct = (GMLConstruct) iterator.next(); if (nextGMLConstruct instanceof Geometry) { Geometry candidate = (Geometry) nextGMLConstruct; System.out.println("Found geometry "+candidate+" "+candidate.getId()); //String candidateGid = candidate.getId(); //System.out.println("With attribute "+nextGMLConstruct.getAttribute(0)); String type = candidate.getXMLDescriptor().getLocalName(); System.out.println("Its a "+type); if(type.equalsIgnoreCase("linearring")){ System.out.println("found a linear ring, its owner was"+candidate.getOwner()); } if(type.equalsIgnoreCase("polygon")){ System.out.println("Building polygon"); GeoPolygon poly = new GeoPolygon(); //candidate = candidate.getOuterBoundry(); Property outerRing = candidate.getPropertyIterator().nextProperty(); GeometryIterator parts = outerRing.getGeometryIterator(); while(parts.hasNext()){ Geometry part = parts.nextGeometry(); System.out.println("part has id " +part.getId()); CoordinateTupleIterator coords = part.getCoordinateTupleIterator(); while(coords.hasNext()){ CoordinateTuple coord = coords.nextCoordinateTuple(); poly.addPoint(coord.getX().getValue().doubleValue(),coord.getY().getValue().doubleValue()); } } layer.addGeoPolygon(poly); } System.out.println(candidate.getXMLDescriptor().getLocalName()); } recursiveAddGeometry(layer,nextGMLConstruct); } } public GeoData readData(int colNumber) { return null; } public Theme getTheme() { return null; } public GeoData[] readData() { return null; } public static void main(String[] args) throws Exception{ GML2Reader gmlReader = new GML2Reader(new URL("http://gemtun:8082/classpath/Schools.xml")); Layer layer = gmlReader.getLayer(); java.awt.Frame f = new java.awt.Frame(); f.setSize(400,400); Viewer view = new Viewer(); f.add(view,"Center"); view.addTheme(new Theme(layer)); f.setVisible(true); } }