使用 Groovy 将 XML 转换为 GeoJson
Converting an XML into GeoJson with Groovy
我一直在尝试使用这个很棒的 example 来读取 XML 文件,将它们转换为 GeoJson 并最终将它们导入 MongoDB.
我的示例 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 创建是否符合我的要求(我实际上认为它没有用)。
//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].@att1;
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'));
创建索引后,我在 collection 中看不到任何记录。我的代码中有明显的错误吗?映射是否递归地在我的多边形中添加坐标数组?有没有办法更好地解决正在发生的问题? (我目前使用的是mongo-java-driver-3.2.1)
非常感谢您的帮助!
所以,想到了这个:
import groovy.json.*
def xml = '''
<root>
<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>
</root>
'''
def doc = new XmlSlurper().parseText(xml)
def polymap = [
type: 'Polygon',
name: doc.AName.head().@att1.text(),
points: doc.AName.collect { [it.Loc1.text() as Double, it.Loc2.text() as Double] }
]
def json = new JsonBuilder(polymap).toString()
assert json == '{"type":"Polygon","name":"sequence","points":[[0.0,0.0],[3.0,6.0]]}'
哪个做你想要的(我认为)
我一直在尝试使用这个很棒的 example 来读取 XML 文件,将它们转换为 GeoJson 并最终将它们导入 MongoDB.
我的示例 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 创建是否符合我的要求(我实际上认为它没有用)。
//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].@att1;
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'));
创建索引后,我在 collection 中看不到任何记录。我的代码中有明显的错误吗?映射是否递归地在我的多边形中添加坐标数组?有没有办法更好地解决正在发生的问题? (我目前使用的是mongo-java-driver-3.2.1)
非常感谢您的帮助!
所以,想到了这个:
import groovy.json.*
def xml = '''
<root>
<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>
</root>
'''
def doc = new XmlSlurper().parseText(xml)
def polymap = [
type: 'Polygon',
name: doc.AName.head().@att1.text(),
points: doc.AName.collect { [it.Loc1.text() as Double, it.Loc2.text() as Double] }
]
def json = new JsonBuilder(polymap).toString()
assert json == '{"type":"Polygon","name":"sequence","points":[[0.0,0.0],[3.0,6.0]]}'
哪个做你想要的(我认为)