4.2.1 Vector data processing¶
Basic information about data sources provides Describe. In the case below Feature Class and Table properties are accessed.
In [19]:
Copied!
nuts_path = r"S:\K155\Public\155ISDP\01\NUTS_RG_20M_2021_3035.shp"
nuts = arcpy.Describe(nuts_path)
nuts.shapeType
nuts_path = r"S:\K155\Public\155ISDP\01\NUTS_RG_20M_2021_3035.shp"
nuts = arcpy.Describe(nuts_path)
nuts.shapeType
Out[19]:
'Polygon'
Fields and their properties can be accessed via fields
property (see Table).
In [20]:
Copied!
len(nuts.fields)
len(nuts.fields)
Out[20]:
11
In [21]:
Copied!
for field in nuts.fields:
print(field.name, field.type)
for field in nuts.fields:
print(field.name, field.type)
FID OID Shape Geometry NUTS_ID String LEVL_CODE SmallInteger CNTR_CODE String NAME_LATN String NUTS_NAME String MOUNT_TYPE SmallInteger URBN_TYPE SmallInteger COAST_TYPE SmallInteger FID_1 String
Data records (rows in attribute table) may be provided by SearchCursor.
In [22]:
Copied!
rows = arcpy.SearchCursor(nuts_path)
for row in rows:
print(row.CNTR_CODE)
break
rows = arcpy.SearchCursor(nuts_path)
for row in rows:
print(row.CNTR_CODE)
break
FR
In [23]:
Copied!
code = 'CZ'
count = 0
rows = arcpy.SearchCursor(nuts_path)
for row in rows:
if row.CNTR_CODE == code:
count += 1
print(f'Number of features with code {code}: {count}')
code = 'CZ'
count = 0
rows = arcpy.SearchCursor(nuts_path)
for row in rows:
if row.CNTR_CODE == code:
count += 1
print(f'Number of features with code {code}: {count}')
Number of features with code CZ: 24
Geometry may be accessed via SHAPE
attribute.
In [24]:
Copied!
area = 0
rows = arcpy.SearchCursor(nuts_path, f"CNTR_CODE = '{code}' AND LEVL_CODE=2")
for row in rows:
area += row.SHAPE.area
print(f"{code} area in km2: {area/1e6}")
area = 0
rows = arcpy.SearchCursor(nuts_path, f"CNTR_CODE = '{code}' AND LEVL_CODE=2")
for row in rows:
area += row.SHAPE.area
print(f"{code} area in km2: {area/1e6}")
CZ area in km2: 78896.87190870794
Let's collect all country codes.
In [25]:
Copied!
codes = []
rows = arcpy.SearchCursor(nuts_path)
count = 0
for row in rows:
if row.CNTR_CODE not in codes:
codes.append(row.CNTR_CODE)
print(sorted(codes))
codes = []
rows = arcpy.SearchCursor(nuts_path)
count = 0
for row in rows:
if row.CNTR_CODE not in codes:
codes.append(row.CNTR_CODE)
print(sorted(codes))
['AL', 'AT', 'BE', 'BG', 'CH', 'CY', 'CZ', 'DE', 'DK', 'EE', 'EL', 'ES', 'FI', 'FR', 'HR', 'HU', 'IE', 'IS', 'IT', 'LI', 'LT', 'LU', 'LV', 'ME', 'MK', 'MT', 'NL', 'NO', 'PL', 'PT', 'RO', 'RS', 'SE', 'SI', 'SK', 'TR', 'UK']
Tip: Use set
instead of list
.
In [26]:
Copied!
import os
arcpy.env.overwriteOutput = True
arcpy.env.workspace = r"C:\Users\martin\Documents\isdp_04" # change path here
if not os.path.exists(arcpy.env.workspace):
os.makedirs(arcpy.env.workspace)
for code in sorted(codes)[:3]:
print("{}...".format(code))
arcpy.analysis.Select(nuts_path, f"nuts_{code}.shp", f"CNTR_CODE = '{code}' AND LEVL_CODE = 2")
import os
arcpy.env.overwriteOutput = True
arcpy.env.workspace = r"C:\Users\martin\Documents\isdp_04" # change path here
if not os.path.exists(arcpy.env.workspace):
os.makedirs(arcpy.env.workspace)
for code in sorted(codes)[:3]:
print("{}...".format(code))
arcpy.analysis.Select(nuts_path, f"nuts_{code}.shp", f"CNTR_CODE = '{code}' AND LEVL_CODE = 2")
AL... AT... BE...
In [27]:
Copied!
for code in sorted(codes)[:3]:
print("{}...".format(code))
arcpy.management.Dissolve(f"nuts_{code}.shp", f"nuts0_{code}.shp", "CNTR_CODE")
for code in sorted(codes)[:3]:
print("{}...".format(code))
arcpy.management.Dissolve(f"nuts_{code}.shp", f"nuts0_{code}.shp", "CNTR_CODE")
AL... AT... BE...