Skip to content

Instantly share code, notes, and snippets.

@stdavis
Created September 1, 2017 23:00
Show Gist options
  • Select an option

  • Save stdavis/d820e65589767aacf23a3d689341660c to your computer and use it in GitHub Desktop.

Select an option

Save stdavis/d820e65589767aacf23a3d689341660c to your computer and use it in GitHub Desktop.
parseXML.py
import arcpy
import xml.etree.ElementTree as etree
from os.path import join
from dateutil.parser import parse
xml = r'Y:\Mineralsoutput.xml'
output_folder = r'C:\Temp'
gdb_name = 'XMLOutput.gdb'
gdb = join(output_folder, gdb_name)
table_name = 'Minerals'
table = join(gdb, table_name)
print('deleting old gdb')
if arcpy.Exists(gdb):
arcpy.management.Delete(gdb)
print('building gdb and table')
arcpy.management.CreateFileGDB(output_folder, gdb_name)
arcpy.management.CreateTable(gdb, table_name)
arcpy.management.AddField(table, 'utmx', 'Long')
arcpy.management.AddField(table, 'utmy', 'Long')
arcpy.management.AddField(table, 'operator', 'Text', field_length=100)
arcpy.management.AddField(table, 'name', 'Text', field_length=100)
arcpy.management.AddField(table, 'status', 'Text', field_length=10)
arcpy.management.AddField(table, 'inspector', 'Text', field_length=100)
arcpy.management.AddField(table, 'app_acr', 'Long')
arcpy.management.AddField(table, 'permittee', 'Text', field_length=100)
arcpy.management.AddField(table, 'mineid', 'Text', field_length=12)
arcpy.management.AddField(table, 'minerals', 'Text', field_length=100)
arcpy.management.AddField(table, 'inspdate', 'Date')
all_xml_fields = ['utmx', 'utmy', 'Operator', 'SiteName', 'Status', 'inspector', 'TotalPermitArea', 'Permittee', 'Permit', 'minerals', 'inspdate']
int_fields = ['utmx', 'utmy']
float_fields = ['app_acr']
date_fields = ['inspdate']
print('parsing xml')
with open(xml, 'r+', encoding='utf-8') as file:
xml_text = file.read()
if not xml_text.startswith('<root>'):
xml_text = xml_text.replace('\n', '')
file.seek(0)
file.truncate()
file.write('<root>{}</root>'.format(xml_text))
tree = etree.fromstring(xml_text)
print('loading data into table')
with arcpy.da.InsertCursor(table, ['*']) as cursor:
for record in tree:
values = []
for field in all_xml_fields:
value = record.get(field)
if field in int_fields:
values.append(int(value))
elif field in float_fields:
values.append(float(value))
elif field in date_fields:
values.append(parse(value))
elif value is not None:
values.append(value.strip())
else:
values.append(value)
cursor.insertRow([1] + values)
print('making xy event layer')
layer = arcpy.management.MakeXYEventLayer(table, 'utmx', 'utmy', 'MinesLayer', arcpy.SpatialReference(26912))
print('copying points')
utm = arcpy.management.CopyFeatures(layer, table + '_UTM')
print('projecting to web mercator')
projected = arcpy.management.Project(utm, table + '_WGS', arcpy.SpatialReference(3857), 'NAD_1983_To_WGS_1984_5')
print('done')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment