Search code examples
mongodbgroovygeojson

Converting an XML into GeoJson with Groovy


I have been trying to use this great example in order to read XML files, convert them into GeoJson and ultimately import them into MongoDB.

My sample XML file looks like this:

<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> 

....

And my GEOJson structure should actually look like this (a typical Polygon with a Single Ring):

{
  type: "Polygon",
  name: "sequence",
  coordinates: [ [ [ 0 , 0 ] , [ 3 , 6 ] , [ 6 , 1 ] , [ 0 , 0  ] ] ]
}

I have started playing with Groovy (which I never used before), but I am not sure whether the actual GeoJson creation does resemble my requirements (I do not actually think it has worked).

    //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'));

While the index has been created, I see no records in my collection. Is there something obviously wrong in my code? Is the mapping recursively adding arrays of coordinates in my Polygon? Is there a way to better troubleshoot what it is going on? (I am currently using mongo-java-driver-3.2.1)

Thank you so much for your help!


Solution

  • so, came up with this:

    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()[email protected](),
        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]]}'
    

    which does what you want (I think)