def purgeGeoprocessingFGDC(table,metadataFile): addMsgAndPrint(' exporting metadata to '+metadataFile) arcpy.ExportMetadata_conversion(table,translator,metadataFile) addMsgAndPrint(' clearing internal metadata') arcpy.ClearMetadata_usgs(table) addMsgAndPrint(' importing metadata from '+metadataFile) arcpy.ImportMetadata_conversion (metadataFile,"FROM_FGDC",table)
def clearImport(objName, gdbObj, gdb, purgeProcessing): gdbName = os.path.basename(gdb)[:-4] xmlFile = xmlDir + '/' + gdbName + objName + '-metadata.xml' #if purgeProcessing: # arcpy.ClearMetadata_usgs(gdbObj,'True','ALWAYS') addMsgAndPrint(' Importing metadata from ' + os.path.basename(xmlFile)) arcpy.ImportMetadata_conversion(xmlFile, 'FROM_FGDC', gdbObj, 'ENABLED') return
def import_XML(): #baseSHP = raw_input('Enter shapefile to use as template: ') #shpPath = os.path.join(base_path, record,record_path,baseSHP) templatePath = os.path.join(base_path, record, 'final_XMLs\\template.xml') #print shpPath # Used to explort a shapefile as a default template '''arcpy.ExportMetadata_conversion (shpPath, ISOTRANSLATOR, templatePath)''' for root, dir, files in os.walk(os.path.join(base_path, record)): #set workspace arcpy.env.workspace = root #get a list of all the SHPs files = arcpy.ListFiles("*.shp") totalTimeTic = time.time() #loop through SHPs and import the metadata for each for f in files: #shapefilePath = os.path.join(base_path,record,record_path,f) tic = time.time() print 'Trying to import XML to: ', f arcpy.ImportMetadata_conversion(templatePath, "FROM_ESRIISO", f, "DISABLED") # Trying to get thumbnail, postponing for now. """# get the map document mxd = arcpy.mapping.MapDocument(blankMXD) # get the data frame df = arcpy.mapping.ListDataFrames(mxd,"*")[0] # create a new layer newlayer = arcpy.mapping.Layer(shapefilePath) # add the layer to the map at the bottom of the TOC in data frame 0 arcpy.mapping.AddLayer(df, newlayer,"BOTTOM") print "creating thumbnail for " + f mxd.makeThumbnail() mxd.save() arcpy.mapping.RemoveLayer(df, newlayer) mxd.save()""" toc = time.time() s = toc - tic m, s = divmod(s, 60) h, m = divmod(m, 60) timeFormat = "%d:%02d:%02d" % (h, m, s) print 'Time elapsed: ', timeFormat totalTimeToc = time.time() s = totalTimeToc - totalTimeTic m, s = divmod(s, 60) h, m = divmod(m, 60) timeFormat = "%d:%02d:%02d" % (h, m, s) print 'Total time elapsed: ', timeFormat
def import_XML(): importXMLpath = raw_input("Enter path to XML template: ") importXMLpath = importXMLpath.replace('"','') #get a list of all the SHPs if record_path == "converted\GISfiles": files = [] for dirpath,dirnames,filenames in os.walk(os.path.join(base_path,record,record_path)): if dirpath.endswith("shapefiles"): for f in filenames: if fnmatch.fnmatch(f,"*.shp"): files.append(os.path.join(dirpath,f)) else: files = glob.glob(os.path.join(arcpy.env.workspace,"*.shp")) totalTimeTic = time.time() #loop through SHPs and import the metadata for each for f in files: #shapefilePath = os.path.join(base_path,record,record_path,f) tic = time.time() print 'Trying to import XML to: \n', f arcpy.ImportMetadata_conversion (importXMLpath,"FROM_FGDC",f, "DISABLED") '''# get the map document mxd = arcpy.mapping.MapDocument(blankMXD) # get the data frame df = arcpy.mapping.ListDataFrames(mxd,"*")[0] # create a new layer newlayer = arcpy.mapping.Layer(shapefilePath) # add the layer to the map at the bottom of the TOC in data frame 0 arcpy.mapping.AddLayer(df, newlayer,"BOTTOM") print "creating thumbnail for " + f mxd.makeThumbnail() mxd.save() arcpy.mapping.RemoveLayer(df, newlayer) mxd.save()''' toc = time.time() s = toc-tic m, s = divmod(s, 60) h, m = divmod(m, 60) timeFormat = "%d:%02d:%02d" % (h, m, s) print 'Time elapsed: ',timeFormat totalTimeToc = time.time() s = totalTimeToc-totalTimeTic m, s = divmod(s, 60) h, m = divmod(m, 60) timeFormat = "%d:%02d:%02d" % (h, m, s) print 'Total time elapsed: ',timeFormat
def metadataFromArcgis(arcgisDatasource): "return a metadata object for the arcgisDatasource" tempXML = tempfile.NamedTemporaryFile(suffix=".xml", delete=False) tempXML.file.write(r'<?xml version="1.0"?><metadata xml:lang="nl"></metadata>') tempXML.file.close() import arcpy #arcpy is only needed here arcpy.ImportMetadata_conversion(arcgisDatasource, "FROM_ARCGIS" , tempXML.name) arcpy.AddMessage("created tempfile: "+ tempXML.name ) return metadata( tempXML.name , True)
def update_layer_metadata(sdeFC, props, srcType): #TEMP_DIR = r"C:\Users\kdb086\Documents\ArcGIS\metadata-files" TEMP_DIR = tempfile.gettempdir() metadataFile = os.path.join(TEMP_DIR, os.path.basename(sdeFC) + '-metadata.xml') #migrationText = " *** Migrated from the L Drive (%s)" % props["Data Source"] migrationText = "<b>Retired L Drive Path: </b> %s" % props["Data Source"] livelinkText = "<b>Livelink Path: </b>" if props["Livelink Link"]: #livelinkText = 'Click <a href="' + props["Livelink Link"] + '">here</a> to go to Livelink' livelinkText = livelinkText + ' <a href="' + props[ "Livelink Link"] + '">' + props["Livelink Link"] + '</a>' else: print('%-60s%s' % (" ", "??? no Livelink Link found")) if os.path.exists(metadataFile): os.remove(metadataFile) # A- export the medata from SDE feature class # print 'exporting the metadata of %s to %s' % (sdeFC, metadataFile) arcpy.ExportMetadata_conversion(Source_Metadata=sdeFC, Translator=METADATA_TRANSLATOR, Output_File=metadataFile) # B- modify metadata # print 'modifying the metadata file [%s]' % (metadataFile) tree = ET.parse(metadataFile) root = tree.getroot() idinfo = root.find('idinfo') dspt = idinfo.find('descript') # B1- add the element if dspt is None: dspt = ET.SubElement(idinfo, 'descript') ET.SubElement(dspt, 'abstract') else: abstract = dspt.find('abstract') if abstract is None: ET.SubElement(dspt, 'abstract') # B2- modify the element text abstract = dspt.find('abstract') if abstract.text is None: # use dot in place of empty space to force CDATA to be recognisable abstract.text = ". " abstract.text = ET.CDATA("%s<p/><p>%s</p><p>%s</p>" % (abstract.text, livelinkText, migrationText)) tree.write(metadataFile) # C- import the modified metadata back to SDE feature class # print 'importing the metadata file [%s] to %s' % (metadataFile, sdeFC) arcpy.ImportMetadata_conversion(Source_Metadata=metadataFile, Import_Type="FROM_FGDC", Target_Metadata=sdeFC, Enable_automatic_updates="ENABLED")
def import_XML(): passed = 1 templatePath = outputXML #get a list of all the SHPs #files = arcpy.ListFiles("*.shp") totalTimeTic = time.time() print 'Full path - ', fullPath #loop through SHPs and import the metadata for each failedMetaImport = [] for root, dirs, files in os.walk(fullPath): #dirname = root.split(os.path.sep)[-1] for f in files: if f.lower().endswith('.shp'): #set workspace arcpy.env.workspace = root ws = arcpy.env.workspace tic = time.time() print 'Trying to import XML to: ', f print templatePath try: arcpy.ImportMetadata_conversion(templatePath, "FROM_FGDC", f, "DISABLED") except: print 'Import failed.' failedMetaImport.append(f) passed = 0 toc = time.time() s = toc-tic m, s = divmod(s, 60) h, m = divmod(m, 60) timeFormat = "%d:%02d:%02d" % (h, m, s) print 'Time elapsed: ',timeFormat totalTimeToc = time.time() s = totalTimeToc-totalTimeTic m, s = divmod(s, 60) h, m = divmod(m, 60) timeFormat = "%d:%02d:%02d" % (h, m, s) print 'Total time elapsed: ',timeFormat failedOutput = os.path.join(fullPath,'failedImport.txt') f = open(failedOutput,'w') for x in failedMetaImport: f.write(x) f.write('\n') f.close() return passed
def save(self, Enable_automatic_updates=False): logwrite("Saving metadata", True) for item in self.items: try: print(item.value) except: print(item) self.elements.write(self.metadata_file) # overwrites itself if self._workspace_type != 'FileSystem': arcpy.ImportMetadata_conversion(self.metadata_file, "FROM_ARCGIS", self.dataset, Enable_automatic_updates=Enable_automatic_updates)
def update_sde_metadata(sdeFC, srcFC): TEMP_DIR = tempfile.gettempdir() metadataFile = os.path.join(TEMP_DIR, os.path.basename(sdeFC) + '-metadata.xml') migrationText = "*** Migrated from the L Drive (%s)" % srcFC if os.path.exists(metadataFile): os.remove(metadataFile) # A- export the medata from SDE feature class print 'exporting the metadata of %s to %s' % (sdeFC, metadataFile) arcpy.ExportMetadata_conversion(Source_Metadata=sdeFC, Translator=METADATA_TRANSLATOR, Output_File=metadataFile) # B- modify metadata print 'modifying the metadata file [%s]' % (metadataFile) tree = ET.parse(metadataFile) root = tree.getroot() idinfo = root.find('idinfo') dspt = idinfo.find('descript') # B1- add the element if dspt is None: dspt = ET.SubElement(idinfo, 'descript') ET.SubElement(dspt, 'abstract') else: abstract = dspt.find('abstract') if abstract is None: ET.SubElement(dspt, 'abstract') # B2- modify the element text abstract = dspt.find('abstract') if abstract.text is None: abstract.text = migrationText elif abstract.text.find(migrationText) == -1: abstract.text = abstract.text + migrationText tree.write(metadataFile) # C- import the modified metadata back to SDE feature class print 'importing the metadata file [%s] to %s' % (metadataFile, sdeFC) arcpy.ImportMetadata_conversion(Source_Metadata=metadataFile, Import_Type="FROM_FGDC", Target_Metadata=sdeFC, Enable_automatic_updates="ENABLED") print 'The metadata of %s is updated' % sdeFC
def updateFeatureClassAbstract(featureClassLocation, featureClassName, abstractText): # set up scratch file location for XML metadata_file = metadataScratchFilePath(featureClassName) if os.path.exists(metadata_file): os.remove(metadata_file) # create XML scratch file arcpy.env.workspace = featureClassLocation arcpy.XSLTransform_conversion(featureClassName, XSLTFilePath(), metadata_file) # update XML file with new abstract updateMetaDataFileAbstract(metadata_file, abstractText) # write updates to the feature class arcpy.ImportMetadata_conversion(metadata_file, "FROM_ARCGIS", featureClassName, Enable_automatic_updates=False)
def copy_metadata(source, destination): """ Copy Metadata from the source feature class to the destination feature class Copying metadata is different in Pro (Python 3), than it is in 10.x (Python 2) source and destination are paths to feature classes (strings). Assumes ArcGIS Metadata format. Choices are: FROM_ARCGIS -> ARCGIS_METADATA FROM_FGDC -> FGDC_CSDGM FROM_ISO_19139 -> ISO19139_UNKNOWN """ print("Copy metadata from {0} to {1}".format(source, destination)) if sys.version_info[0] < 3: arcpy.ImportMetadata_conversion(source, "FROM_ARCGIS", destination) else: dest = arcpy.metadata.Metadata(destination) dest.importMetadata(source, "ARCGIS_METADATA") dest.save()
def import_XML(): templatePath = outputXML totalTimeTic = time.time() print 'Full path - ', fullPath #loop through SHPs and import the metadata for each failedMetaImport = [] for root, dirs, files in os.walk(fullPath): dirname = root.split(os.path.sep)[-1] #set workspace arcpy.env.workspace = os.path.join(defaultPath, record, dirname) ws = arcpy.env.workspace #print ws for f in files: if f.lower().endswith('.shp'): tic = time.time() print 'Trying to import XML to: ', f print templatePath try: arcpy.ImportMetadata_conversion(templatePath, "FROM_FGDC", f, "DISABLED") except: failedMetaImport.append(f) toc = time.time() s = toc - tic m, s = divmod(s, 60) h, m = divmod(m, 60) timeFormat = "%d:%02d:%02d" % (h, m, s) print 'Time elapsed: ', timeFormat totalTimeToc = time.time() s = totalTimeToc - totalTimeTic m, s = divmod(s, 60) h, m = divmod(m, 60) timeFormat = "%d:%02d:%02d" % (h, m, s) print 'Total time elapsed: ', timeFormat
def save(self, to_feature_class=True): logwrite("Saving metadata", True) for item in self.items: item._write() self.elements.write(self.metadata_file) # overwrites itself if to_feature_class and self.feature_class: # if we want to save it out to the feature class and feature class is defined arcpy.ImportMetadata_conversion( self.metadata_file, "FROM_FGDC", self.feature_class, Enable_automatic_updates=False ) # Leave Enable_automatic_updates as False because it will undo some of what we set and we just exported off of this dataset anyway! if self.feature_layer: # if we started with a feature layer, we need to recreate it with our new metadata arcpy.Delete_management( self.feature_layer) # delete the existing feature layer arcpy.MakeFeatureLayer_management( self.feature_class, self.feature_layer ) # and turn our temporary feature class into the feature layer
def save(self, overwrite=True, Enable_automatic_updates=False): logwrite("Saving metadata", True) for item in self.items: try: print(item.value) except: print(item) #for item in self.items: # item._write() self.elements.write(self.metadata_file) # overwrites itself out_xml_path = os.path.join(os.path.dirname(self.metadata_file), str(uuid.uuid4()) + '.xml') shutil.copy(self.metadata_file, out_xml_path) if self._workspace_type != 'FileSystem' and overwrite: arcpy.ImportMetadata_conversion(self.metadata_file, "FROM_ARCGIS", self.dataset, Enable_automatic_updates=Enable_automatic_updates) return out_xml_path
def save(self, Enable_automatic_updates=False): """ Save pending edits to file If feature class, import temporary XML file back into GDB :param Enable_automatic_updates: boolean :return: """ self.logger.info("Saving metadata") # Write meta-metadata self.meta_style = "ISO 19139 Metadata Implementation Specification" if not self.meta_create_date: self.meta_create_date = datetime.now().date().isoformat() if not self.meta_create_time: self.meta_create_time = datetime.now().time().isoformat() self.meta_modification_date = datetime.now().date().isoformat() self.meta_modification_time = datetime.now().time().isoformat() self.meta_format = "1.0" self.meta_profile = "ISO19139" self.meta_publish_status = "editor:arcpy_metadata" for item in self.items: # TODO: What's going on here? try: self.logger.debug(item.value) except: self.logger.warn(item) self.elements.write(self.metadata_file) # overwrites itself if self._workspace_type != 'FileSystem': if Enable_automatic_updates: updates = 'ENABLED' else: updates = 'DISABLED' arcpy.ImportMetadata_conversion(self.metadata_file, "FROM_ARCGIS", self.dataset, Enable_automatic_updates=updates)
def setMetadataMXD(self): arcpy.ImportMetadata_conversion(self.pathxml, "FROM_FGDC", self.pathmxd, 'ENABLED')
arcpy.USGSMPTranslator_conversion(os.path.join(workDir, xmlFileMR), '#', '#', '#', logFileName) for aline in open(logFileName, 'r').readlines(): addMsgAndPrint(aline[:-1]) addMsgAndPrint(' ') logFile = open(logFileName, 'a') # import to geodatabase as whole arcpy.env.workspace = workDir supplementaryInfo = gdb + gdbDesc0a + gdbDesc1 + gdbDesc2 dom = addSupplinf(domMR, supplementaryInfo) addMsgAndPrint(' Importing XML to metadata for GDB as a whole') writeDomToFile(workDir, dom, xmlFileGdb) try: arcpy.ImportMetadata_conversion(os.path.join(workDir, xmlFileGdb), 'FROM_FGDC', inGdb, 'ENABLED') except: addMsgAndPrint('Failed to import ' + os.path.join(workDir, xmlFileGdb)) # import to tables arcpy.env.workspace = inGdb tables = arcpy.ListTables() for aTable in tables: revisedMetadata = gdb + '-' + aTable + '.xml' addMsgAndPrint(' Creating XML for ' + aTable) dom = xml.dom.minidom.parse(os.path.join(workDir, xmlFileMR)) dom = titleSuffix(dom, ': table ' + aTable) supplementaryInfo = 'Table ' + aTable + gdbDesc0b + gdbDesc1 + gdbDesc2 dom = addSupplinf(dom, supplementaryInfo) dom = updateTableDom(dom, aTable, logFile) addMsgAndPrint(' Importing XML to metadata for table ' + aTable)
# Generate thumbnail lyrThumb = arcpy.mapping.Layer(shp) arcpy.mapping.AddLayer(df, lyrThumb, 'BOTTOM') arcpy.mapping.ExportToJPEG( mxd, os.path.join(os.environ['TEMP'], 'thumb.jpg'), df, 200, 133, '', False, '24-BIT_TRUE_COLOR', 60) for lyr in arcpy.mapping.ListLayers(df): arcpy.mapping.RemoveLayer(df, lyr) # Import metadata metaFile = os.path.join(basePath, dir, shp + '.xml') if platform.architecture()[0] != '64bit': if os.path.exists(metaFile): os.remove(metaFile) arcpy.ImportMetadata_conversion( os.path.join(basePath, 'tools', 'nemeta_template.xml'), 'FROM_ISO_19139', shp, 'ENABLED') # Fix metadata metaDoc = ElementTree.parse(metaFile) # Fix title title = re.sub('ne_(\d+m)_(.*).shp', r'\2 (1:\1)', shp) title = title.replace('_', ' ') title = re.sub('(admin \d) (.*) (\(.*\))', r'\2, \1 \3', title).title() metaDoc.findall('.//itemProps/itemName')[0].text = title del metaDoc.findall('.//itemProps/itemName')[0].attrib['Sync'] metaDoc.findall('.//itemProps/itemName')[0].attrib['Sync'] = 'FALSE' metaDoc.findall('.//dataIdInfo/idCitation/resTitle')[0].text = title del metaDoc.findall( './/dataIdInfo/idCitation/resTitle')[0].attrib['Sync']
# Author: Julien Moura (@geojulien) # # Python: 2.7.x # Created: 10/04/2015 # Updated: 10/04/2015 # # Licence: GPL 3 # ----------------------------------------------------------------------------- ############################################################################### ########### Libraries ############# ################################### # 3rd party libraries import arcpy from arcpy import env ############################################################################### ############ Main program ############ ################################### arcpy.ImportMetadata_conversion("c:/data/streams.shp", "FROM_FGDC", "streams") ############################################################################### ###### Stand alone program ######## ################################### if __name__ == '__main__': """ standalone execution for testing """ pass
spatialFolder = os.path.join(newFolder, "spatial") env.workspace = spatialFolder shpList = arcpy.ListFeatureClasses( "soilmu_a*", "Polygon") try: if len(shpList) == 1: muShp = shpList[0] AddMsgAndPrint( "\t\tImporting metadata for " + muShp, 0) metaData = os.path.join( newFolder, "soil_metadata_" + areaSym.lower() + ".xml") arcpy.ImportMetadata_conversion( metaData, "FROM_FGDC", os.path.join(spatialFolder, muShp), "ENABLED") del spatialFolder, muShp, metaData except: AddMsgAndPrint( "\t\tImporting metadata for " + muShp + " Failed. ", 0) pass # end of successful zip file download break # Zip file size is empty. Attempt again if 2nd attempt has not been executed else: if i == 0:
#env.workspace="F:\\SDM_paper\\maxent\\Maxent_run\\Species_richness\\clusters\\" WEA=["DE","MS","NC","NJ","NY","RI","VA"] #WEA=["MS","NJ","NY","RI","VA"] #WEA=["DE","MS","NJ","NY","RI",] env.workspace="E:\\Latest_BOEM_Data\\WEA_Rim_Lease_Blocks\\" out_workspace="F:\\BOEM final report\\GIS_data\\" raster="F:\\BOEM final report\\Regional_layers\\Sediments\\us_am_s_e" #clipf="F:\\BOEM final report\\Regional_layers\\CRM_and_derived\\30m_contour_rg.shp" for a in WEA: for shape in arcpy.ListFeatureClasses(): print (shape) if a in shape: #bname=arcpy.Describe(shape).basename # name="aspect_crm_"+a+".shp" name="us_am_s_e_"+a print(name) EM=ExtractByMask(raster,shape) out=out_workspace+a+"\\Sediment\\"+name # out=out_workspace+a+"\\CRM_derived_products\\" print(out) # EMsave=os.path.join(out,name) #print(EMsave) EM.save(out) arcpy.ImportMetadata_conversion(raster, "FROM_ARCGIS", out) # arcpy.Clip_analysis(clipf,shape,out,"") else: pass