我一直在嘗試使用這個偉大的example來讀取XML文件,將它們轉換爲GeoJson並最終將它們導入到MongoDB中。使用Groovy將XML轉換爲GeoJson
我的示例XML文件是這樣的:
<AName att1="sequence" att2="xx" att3="xxx">
<Loc1>0</Loc1>
<Loc2>0</Loc2>
</AName>
<AName att1="sequence" att2="xx" att3="xxx">
<Loc1>3</Loc1>
<Loc2>6</Loc2>
</AName>
....
而且我GeoJSON的結構實際上應該是這樣的(一個典型的Polygon with a Single Ring):
{
type: "Polygon",
name: "sequence",
coordinates: [ [ [ 0 , 0 ] , [ 3 , 6 ] , [ 6 , 1 ] , [ 0 , 0 ] ] ]
}
我已經開始使用Groovy(我以前從未使用過),但我不確定實際的GeoJson創建是否與我的要求相似(我實際上並不認爲它有我rked)。
//Here I am using the XmlSlurper() to parse the xml file
def file= new XmlSlurper().parse(new File('MyFile.xml'));
def a = file.AName[0][email protected];
println("AName " + a);
def loc1= file.depthFirst().find() {it.name() == 'Loc1'};
def loc2= file.depthFirst().find() {it.name() == 'Loc2'};
//Here I am converting the Java representation of my XML file to a GeoJson format
file.node.each {child ->
Map myMap= [type: 'Polygon',
name : a,
loc: [coordinates:
[Double.valueOf(loc1), Double.valueOf(loc2)],
]]
//Finally I am inserting the BasicDBObject and creating the 2dsphere index
collection.insert(new BasicDBObject(myMap));
collection.createIndex(new BasicDBObject('loc', '2dsphere'));
雖然該索引已創建,但在我的收藏中看不到記錄。我的代碼中有明顯的錯誤嗎?映射是否遞歸地在我的Polygon中添加座標數組?有沒有更好的故障排除方法? (我目前使用的是mongo-java-driver-3.2.1)
非常感謝您的幫助!
你可以張貼一些實際的XML和JSON實際你希望它可以產生? –
@time_yates,我已經闡明瞭我想實現的GeoJson的類型。希望這可以幫助。 – paranza
但是XML中的座標在哪裏? X屬性在哪裏?您的示例xml發送與期望的輸出沒有任何相似之處,或者代碼 –