Last active
September 26, 2019 20:48
-
-
Save jccartwright/f27257b700a9445d7090ca00e02ad89c to your computer and use it in GitHub Desktop.
convert Okeanos Explorer observations XML to GeoJSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import groovy.json.* | |
def observationsUrl = 'url_for_xml_file' | |
xmlString = observationsUrl.toURL().text | |
// don't know why can't read URL directly | |
observations = new XmlSlurper().parseText(xmlString) | |
def features = [] | |
def output = [ | |
"type": "FeatureCollection", | |
"features": features | |
] | |
observations.marker.each { | |
def lon = it.@lon.text().toDouble() | |
def lat = it.@lat.text().toDouble(); | |
// treat all properties as Strings since they may contain 'NA' for numeric fields | |
features << [ | |
"type": "Feature", | |
"geometry": [ | |
"type": "Point", | |
"coordinates": [lon, lat] | |
], | |
"properties": [ | |
"cruiseID": it.@cruiseID.text(), | |
"dateTime": it.@dateTime.text(), | |
"gyro_hdg": it.@gyro_hdg.text(), | |
"wind_dir_true": it.@wind_dir_true.text(), | |
"wind_spd_true_kts": it.@wind_spd_true_kts.text(), | |
"wind_dir_rel": it.@wind_dir_rel.text(), | |
"wind_spd_rel_kts": it.@wind_spd_rel_kts.text(), | |
"air_temp": it.@air_temp.text(), | |
"rel_humidity": it.@rel_humidity.text(), | |
"baro_press_mb": it.@baro_press_mb.text(), | |
"cog": it.@cog.text(), | |
"sog": it.@sog.text(), | |
"depth_m": it.@depth_m.text(), | |
"salinity": it.@salinity.text(), | |
"conductivity": it.@conductivity.text(), | |
"sea_surface_temp": it.@sea_surface_temp.text() | |
] | |
] | |
} | |
def json = JsonOutput.toJson(output) | |
println JsonOutput.prettyPrint(json) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment