2013-02-20 21 views
0

我用Geotools 8.4爲OS Mastermap寫了一個GML文件解析器。有一部分我仍然有問題。在XSD的這sniplet:如何使用Geotools編寫GML解析器?

<complexType name="RoadLinkType"> 
    <complexContent> 
     <extension base="osgb:AbstractFeatureType"> 
      <sequence> 
       <element name="polyline" type="gml:GeometryPropertyType"/> 
       <element name="directedNode" type="osgb:directedNodeAssociationType" minOccurs="2" maxOccurs="2"/> 
      </sequence> 
     </extension> 
    </complexContent> 
</complexType> 

的部分來自GML文件:

<osgb:RoadLink fid='osgb4000000023204016'> 
    <osgb:polyline> 
     <gml:LineString srsName='osgb:BNG'> 
      <gml:coordinates>516609.000,257678.000 516615.000,257733.000 516618.000,257786.000</gml:coordinates> 
     </gml:LineString> 
    </osgb:polyline> 
    <osgb:directedNode orientation='-' xlink:href='#osgb4000000027916595'/> 
    <osgb:directedNode orientation='+' gradeSeparation='1' xlink:href='#osgb4000000028203009'/> 
</osgb:RoadLink> 

它被解析接近直角,即折線是正確的(我得到一個類com.vividsolutions.jts.geom .LineString),但我只得到一個(而不是兩個)directedNode。

我的代碼:

GML gml = new GML(Version.GML3); 
CRSAuthorityFactory crsFac = ReferencingFactoryFinder.getCRSAuthorityFactory("EPSG", null); 
CoordinateReferenceSystem osgbCrs = crsFac.createCoordinateReferenceSystem("EPSG:27700"); 
gml.setCoordinateReferenceSystem(osgbCrs); 
SimpleFeatureIterator iter = gml.decodeFeatureIterator(in); 
while (iter.hasNext()) 
{ 
    SimpleFeature feature = iter.next(); 
    System.out.println("id: " + feature.getID()); 
    System.out.println("polyline: " + feature.getAttribute("polyline")); 
    Object dnode = feature.getAttribute("directedNode"); 
    System.out.println("directedNode: '" + dnode + "'\t" + dnode.getClass()); 
} 

和輸出:

id: osgb4000000023204016 
polyline: LINESTRING (516609 257678, 516615 257733, 516618 257786) 
directedNode: '{orientation=-, href=#osgb4000000027916595}' class java.util.HashMap 

我在做什麼錯?爲什麼我不能同時使用directedNode?我怎樣才能綁定到真正的Java對象?

+0

我也發佈在gis.stackexchange上,因爲我不確定它更適合哪裏:http://gis.stackexchange.com/questions/52418/how-to-write-gml-parser-with-geotools – Burkhard 2013-02-20 13:04:25

回答

2

在循環中你打電話Object dnode = feature.getAttribute("directNode");。如SimpleFeature的文檔中所述,該調用將只返回一個節點,即列表中的第一個或最後一個,文檔沒有說明。如果你想檢索所有的節點,你應該打電話給getAttributes()。雖然你需要自己檢查名字,但它會解決你的問題。

雖然過了一段時間,但它仍然可能與讀者有關!

+0

謝謝爲你的努力(儘管我不再需要它)。 – Burkhard 2014-01-06 15:18:12

+0

不客氣。您是否希望我在GIS Stackexchange站點上發佈答案,或者是否想鏈接到這個答案,編寫自己的答案? – Eric 2014-01-07 06:46:24

+0

你可以做到。你值得享有聲譽;) – Burkhard 2014-01-07 07:15:48