import arcpy
import tables
from tables import *
def feature_class_to_hdf5(featureClass, tableName, groupName='', indexFields=[]):
'''
TODO: add polygon and line geometry type support
TODO: add blob field
TODO: add raster support
TODO: add Date logic
'''
try:
#Validate feature class
desc = arcpy.Describe(featureClass)
if desc.dataType != 'FeatureClass':
raise ValueError('featureClass must be a featureClass')
if desc.featureType != 'Simple':
raise ValueError('feature_class_to_table only support simple feature types')
if desc.shapeType != 'Point':
raise ValueError('sorry')
#Construct output location for hdf5
output_file_name = os.path.splitext(os.path.split(desc.catalogPath)[1])[0]
if '.gdb' in desc.catalogPath:
output_directory = os.path.split(os.path.split(desc.catalogPath)[0])[0]
elif '.shp' in desc.catalogPath:
output_directory = os.path.split(desc.catalogPath)[0]
output_hd5_path = os.path.join(output_directory, output_file_name + '.h5')
#Map ArcGIS Field Types to PyTable Field Types
fields_dictionary = {}
fields_dictionary['OID'] = Int32Col(dflt=1, pos = 0)
fields_dictionary['X'] = Float64Col(dflt=1, pos = 1)
fields_dictionary['Y'] = Float64Col(dflt=1, pos = 2)
h5f_field_names = ['OID', 'X', 'Y']
arc_field_names = ['OID@', 'SHAPE@X', 'SHAPE@Y']
for f in desc.fields:
if f.type in ['String','GUID']:
fields_dictionary[str(f.name)] = StringCol(itemsize=f.length, dflt="n/a", pos=len(arc_field_names))
elif f.type == 'Single':
fields_dictionary[str(f.name)] = Float32Col(dflt=1, pos=len(arc_field_names))
elif f.type == 'Double':
fields_dictionary[str(f.name)] = Float64Col(dflt=1, pos=len(arc_field_names))
elif f.type == 'SmallInteger':
fields_dictionary[str(f.name)] = Int16Col(dflt=1, pos=len(arc_field_names))
elif f.type == 'Integer':
fields_dictionary[str(f.name)] = Int32Col(dflt=1, pos=len(arc_field_names))
elif f.type == 'Date':
fields_dictionary[str(f.name)] = Time64Col(dflt=1, pos=len(arc_field_names)) #Needs test...
else:
continue
arc_field_names.append(str(f.name))
h5f_field_names.append(str(f.name))
#Create PyTables DescriptionClass
DescriptionClass = type(tableName, (IsDescription,), fields_dictionary)
with tables.openFile(output_hd5_path, 'w', title=featureClass) as h5_file:
filters = Filters(complib='blosc', complevel=1)
h5_feature_class = h5_file.createTable('/', tableName, DescriptionClass, filters=filters)
with arcpy.da.SearchCursor(featureClass, field_names=arc_field_names) as cursor:
for r in cursor:
new_row = h5_feature_class.row
for i, fieldName in enumerate(h5f_field_names):
try:
new_row[fieldName] = r[i]
except TypeError as e:
if 'unicode' in str(e).lower():
new_row[fieldName] = unicodedata.normalize('NFKD', r[i]).encode('ascii','ignore')
else:
raise
new_row.append()
h5_file.flush()
except Exception as e:
print e
raise
.NET
(1)
AIR
(1)
Algorithms
(1)
ArcGIS
(2)
ArcGIS Server
(7)
ArcMap
(2)
ArcObjects
(6)
arcpy
(1)
Arrays
(1)
AS3
(1)
Bing
(1)
C#
(8)
Clean Code
(1)
Clustering
(1)
COM
(1)
Design Patterns
(1)
Developer Interviews
(1)
Django
(1)
ESRI Developer Summit
(3)
ESRI Flex API
(1)
Flash
(1)
Flex
(7)
fuzzy
(1)
Geometry
(1)
Geoprocessing
(3)
Google
(1)
Imaging
(1)
JavaScript
(2)
Map Tiles
(2)
Mobile
(1)
Nokia
(1)
Offline Mapping
(2)
PIL
(1)
Pixel Bender
(1)
Presentations
(1)
PSU
(2)
PyTables
(1)
Python
(18)
Rasters
(2)
Server Object Extensions
(4)
Sorting
(1)
Spatial Analysis
(1)
SQLite
(1)
Sublime Text 2
(1)
VB
(2)
War
(1)
Where Camp
(1)
Yahoo
(1)
27.2.13
Converting ArcGIS Feature Class to PyTables
Here is an example of converting a point geometry type ArcGIS Feature Class to a PyTables table. I decided to avoid using the arcpy.da.FeatureClassToNumpyArray, because I want more flexibility on how data is converted (e.g. geometry types, blobs, rasters, dates). Right now this only handles a point feature class, but I hope to add in other geometry types and make is into a module:
Subscribe to:
Post Comments (Atom)
This comment has been removed by the author.
ReplyDeletenice article
ReplyDeletevery useful information.
keep sharing.
Best python programming Training
Great post!! Thanks for sharing...
ReplyDeletePython Course in Bangalore
Thanks for sharing this informative post. MEAN Stack Classes in Pune
ReplyDeleteNice article. Thanks for sharing.
ReplyDeletealso, check Python Classes in Pune