Tuesday, 20 August 2019

arcpy - Getting value from Metadata in Python Script for attribute?


I am working on a python script to create some feature classes from dbf tables. As part of the script I would like to pull two values (FGDC "publication date" and ArcGIS Spatial Reference ArcGIS Coordiante System "Projection") from the metadata of a linear referencing feature class and write them to a field in the feature class. I've looked at some of the modules that parse .xml files, but the linear referencing feature class doesn't have a standalone .xml. Below is the part of the script where i'd like to pull and calculate the values.


#Pull COORDSYS and MAPVERSION from RCI Metadata

#Calculate Values for Coordsys, Mapsource, MapVersion, publish date, CAR xfer date, coord xfer date
today = datetime.date.today()

pub_date = today.strftime("%m/%d/%y")
arcpy.CalculateField_management(input,"COORDSYS",XXX,"PYTHON_9.3")
arcpy.CalculateField_management(input,"MAPSOURCE",mapsource_ON,"PYTHON_9.3")
arcpy.CalculateField_management(input,"MAPVERSION",XXX,"PYTHON_9.3")
arcpy.CalculateField_management(input,"DTPUBLISHE",pub_date,"PYTHON_9.3")
arcpy.CalculateField_management(input,"DTCARXTRCT",pulldate,"PYTHON_9.3")
arcpy.CalculateField_management(input,"DTCOORDXTR",pub_date,"PYTHON_9.3")

Answer



Here is the code that finally did the trick. This is the same method proposed by Helene above. Any comments on how to improve this are welcome.


    #Create XML from RCI Basemap Metadata

translator = "C:\Program Files (x86)\ArcGIS\Desktop10.2\Metadata\Translator\ARCGIS2FGDC.xml"
arcpy.ExportMetadata_conversion(roads,translator,arcpy.env.workspace+"\\"+"roads_Export.xml")

#Get MAPVERSION from RCI Basemap
from xml.etree.ElementTree import ElementTree
from xml.etree.ElementTree import Element, SubElement

xmlfile = arcpy.env.workspace+"\\"+"roads_Export.xml"

tree = ElementTree()

tree.parse(xmlfile)

spot = tree.find("idinfo/citation/citeinfo/pubdate")
pub_date = spot.text

year = pub_date[0:4]
month = pub_date[4:6]
month_num = int(month)

if 0 < month_num < 3:

version = year+"Q1"
elif 3 < month_num < 6:
version = year+"Q2"
elif 6 < month_num < 9:
version = year+"Q3"
elif 9 < month_num < 12:
version = year+"Q4"

#Calculate Values for Coordsys, Mapsource, MapVersion, publish date, CAR xfer date, coord xfer date


arcpy.CalculateField_management(input,"MAPVERSION",'"{0}"'.format(version),"PYTHON")

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...