Last active
September 26, 2019 20:48
-
-
Save jccartwright/50c68402dab6b763e9e2340c73842347 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 xml.etree.ElementTree as ET | |
import json | |
def parse_xml(xmlfile): | |
features = [] | |
output = { | |
"type": "FeatureCollection", | |
"features": features | |
} | |
# create element tree object | |
tree = ET.parse(xmlfile) | |
# <markers> | |
root = tree.getroot() | |
for item in root: | |
# reduce precision on coordinates. original had 10 decimal places. 5 places should give ~1m or less | |
lon = round(float(item.attrib["lon"]), 5) | |
lat = round(float(item.attrib["lat"]), 5) | |
features.append({ | |
"type": "Feature", | |
"geometry": { | |
"type": "Point", | |
"coordinates": [lon, lat] | |
}, | |
"properties": { | |
"cruiseID": item.attrib["cruiseID"], | |
"dateTime": item.attrib["dateTime"], | |
"gyro_hdg": item.attrib["gyro_hdg"], | |
"wind_dir_true": item.attrib["wind_dir_true"], | |
"wind_spd_true_kts": item.attrib["wind_spd_true_kts"], | |
"wind_dir_rel": item.attrib["wind_dir_rel"], | |
"wind_spd_rel_kts": item.attrib["wind_spd_rel_kts"], | |
"air_temp": item.attrib["air_temp"], | |
"rel_humidity": item.attrib["rel_humidity"], | |
"baro_press_mb": item.attrib["baro_press_mb"], | |
"cog": item.attrib["cog"], | |
"sog": item.attrib["sog"], | |
"depth_m": item.attrib["depth_m"], | |
"salinity": item.attrib["salinity"], | |
"conductivity": item.attrib["conductivity"], | |
"sea_surface_temp": item.attrib["sea_surface_temp"] | |
} | |
}) | |
return output | |
def main(): | |
points = parse_xml('path_to_xml_file') | |
print(json.dumps(points, indent=2)) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment