Skip to content

Instantly share code, notes, and snippets.

@AlexArcPy
Last active July 14, 2022 08:56
Show Gist options
  • Save AlexArcPy/07dfe5b85c526ef93fde90512ce6f491 to your computer and use it in GitHub Desktop.
Save AlexArcPy/07dfe5b85c526ef93fde90512ce6f491 to your computer and use it in GitHub Desktop.
Read file geodatabase domains with OGR without using arcpy and find what fields have assigned domains
from __future__ import print_function
import json
import xml.etree.ElementTree as ET
import ogr
gdb_path = r'C:\GIS\data\Adv.gdb'
ds = ogr.Open(gdb_path)
res = ds.ExecuteSQL('select * from GDB_Items')
res.CommitTransaction()
for i in xrange(0, res.GetFeatureCount()):
item = json.loads(
res.GetNextFeature().ExportToJson())['properties']['Definition']
if item:
xml = ET.fromstring(item)
if xml.tag == 'GPCodedValueDomain2':
print(xml.find('DomainName').text)
print(xml.find('Description').text)
print(xml.find('FieldType').text)
for table in xml.iter('CodedValues'):
for child in table:
print(child.find('Code').text, child.find('Name').text)
print()
if xml.tag == 'GPRangeDomain2':
print(xml.find('DomainName').text)
print(xml.find('Description').text)
print(xml.find('FieldType').text)
print(xml.find('MinValue').text)
print(xml.find('MaxValue').text)
# Domain1
# Desc1
# esriFieldTypeString
# a aa
# b bb
# Domain2
# Desc2
# esriFieldTypeInteger
# 1 aa
# 2 bb
# 3 cc
# Domain3
# Desc3
# esriFieldTypeInteger
# 0
# 100
from __future__ import print_function
import json
import xml.etree.ElementTree as ET
import ogr
gdb_path = r'C:\GIS\data\Adv.gdb'
ds = ogr.Open(gdb_path)
res = ds.ExecuteSQL('select * from GDB_Items')
res.CommitTransaction()
for i in xrange(0, res.GetFeatureCount()):
item = json.loads(
res.GetNextFeature().ExportToJson())['properties']['Definition']
if item:
xml = ET.fromstring(item)
if xml.tag == 'DEFeatureClassInfo' and xml.find('Name').text == 'Fc1':
field_infos = xml.find('GPFieldInfoExs')
for field in field_infos.getchildren():
domain = field.find('DomainName').text if field.find('DomainName') is not None else ''
print(field.find('Name').text, '<-', domain)
# OBJECTID <-
# SHAPE <-
# Field5 <- Domain1
# Field6 <-
# SHAPE_Length <-
# SHAPE_Area <-
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment