Skip to content

Instantly share code, notes, and snippets.

View AlexArcPy's full-sized avatar

Alexey Tereshenkov AlexArcPy

View GitHub Profile
@AlexArcPy
AlexArcPy / arcpy_select_distinct.py
Last active January 31, 2016 08:11
Sample arcpy helper function
def select_distinct(input_fc,input_field):
'''returns a list of unique values in a field sorted'''
sql = (None, 'GROUP BY {0}'.format(input_field))
cur = arcpy.da.SearchCursor(in_table=input_fc,field_names=["{0}".format(input_field)],
sql_clause=sql)
unique_values = [f[0] for f in cur]
return sorted(unique_values)
@AlexArcPy
AlexArcPy / get_not_null_fields.py
Last active January 31, 2016 08:10
Sample arcpy helper function - get not null fields
import arcpy
def get_fields(input_fc,only_field_type="String",not_null=True):
'''returns list of field names of specified data type and contain at
least one not NULL value'''
if not not_null:
all_fields = [field.name for field in arcpy.ListFields(in_fc,field_type=only_field_type)]
return all_fields
@AlexArcPy
AlexArcPy / export_attribute_table_to_excel.py
Created February 26, 2016 19:18
Export attribute table of a fc to an Excel workbook with xlsxwriter site-package
import xlsxwriter
import arcpy
workbook = xlsxwriter.Workbook('ResearchAreas.xlsx')
worksheet = workbook.add_worksheet()
fc = r"C:\ArcTutor\GP Service Examples\ClipAndShip\ToolData\Zion.gdb\Research_areas"
fields = [f.name for f in arcpy.ListFields(fc) if f.name.upper() not in ("OBJECTID","SHAPE")]
rows = [r for r in arcpy.da.SearchCursor(fc,fields)]
rows_structured = [list(elem) for elem in rows]
@AlexArcPy
AlexArcPy / geodatabase_class.py
Last active February 5, 2022 06:13
OOP approach for arcpy users
import arcpy
import os
########################################################################
class FeatureClass(object):
"""FeatureClass object"""
#----------------------------------------------------------------------
def __init__(self,path):
"""Constructor for FeatureClass"""
@AlexArcPy
AlexArcPy / geodatabase_describe.py
Created March 1, 2016 16:27
Accessing properties of a geodatabase
import arcpy
path = r"C:\ArcTutor\Editing\Zion.gdb"
#obtaining individual properties
release = arcpy.Describe(path).release
workspaceType = arcpy.Describe(path).workspaceType
workspaceFactoryProgID = arcpy.Describe(path).workspaceFactoryProgID
#obtaining a dictionary of properties
@AlexArcPy
AlexArcPy / geodatabase_class_instance.py
Created March 6, 2016 06:57
OOP approach for arcpy: class instance
gdb = Geodatabase(path=r"C:\ArcTutor\Editing\Zion.gdb",initialize=True)
print gdb.featureClassesFullPaths
#[u'C:\\ArcTutor\\Editing\\Zion.gdb\\Springs',
#u'C:\\ArcTutor\\Editing\\Zion.gdb\\Tracts',
#u'C:\\ArcTutor\\Editing\\Zion.gdb\\Trails',
#u'C:\\ArcTutor\\Editing\\Zion.gdb\\Roads']
fcs = gdb.feature_classes_objects
print fcs
@AlexArcPy
AlexArcPy / unicodecsv_sample.py
Created March 19, 2016 20:21
unicodecsv - read csv files
import unicodecsv
csv_file = r"C:\GIS\temp\graffiti.csv"
#read as is
with open(csv_file,"rb") as f:
reader = unicodecsv.DictReader(f)
reader_rows = list(reader)
print reader_rows[:3]
@AlexArcPy
AlexArcPy / arcgis_server_stress_test.py
Last active May 23, 2023 04:45
Sample: Stress testing ArcGIS Server map service with the Python multiprocessing module and ArcREST exporting many map images in parallel
import multiprocessing
from arcrest import AGSTokenSecurityHandler
from arcrest.ags.server import Server
from arcrest.common.geometry import Envelope
ags_admin_url = r"http://localhost:6080/arcgis/admin"
ags_security_handler = AGSTokenSecurityHandler(username="username",
password="password",
org_url=ags_admin_url)
@AlexArcPy
AlexArcPy / buffer_single_shapefile_diff_param_values.py
Last active April 20, 2016 20:08
Call FME workbench from Python: FMEWorkspaceRunner
import sys
sys.path.append(r"C:\Program Files (x86)\FME\fmeobjects\python27")
import fmeobjects
#----------------------------------------------------------------------
def buffer_single_shapefile_diff_param_values():
"""
Processes the same shapefile specified in the FME workbench project
creating multiple output shapefiles with the distance appended to the
output file.
@AlexArcPy
AlexArcPy / read_features.py
Created April 20, 2016 19:50
Calling fmeobjects with Python: FMEUniversalReader and FMEUniversalWriter
import sys
sys.path.append(r"C:\Program Files (x86)\FME\fmeobjects\python27")
from fmeobjects import *
import os
root_folder = r"C:\GIS\fme"
#----------------------------------------------------------------------
def read_features():
"""Reads all features within input shapefile, projects it and writes