Skip to content

Instantly share code, notes, and snippets.

@sfabijanski
Created April 24, 2020 21:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sfabijanski/e2fbec8150d67a48dd105b9218517606 to your computer and use it in GitHub Desktop.
Save sfabijanski/e2fbec8150d67a48dd105b9218517606 to your computer and use it in GitHub Desktop.
A basic Esri Python Toolbox which takes an input polygon and clips a network stored featureclass that icludes parcels features, returning the clipped features back to the caler.
# A basic Python Toolbox script that takes a polygon as input and returns back
# a collection of parcel features clipped by the incoming polygon
# refer to https://gist.github.com/sfabijanski/b599ad795c0f573d88202e122ab19275 for information
# on the polygon template
# import arcpy
from arcpy import (
AddMessage,
da,
Delete_management,
Dissolve_management,
env,
Clip_analysis,
GetMessage,
GetParameter,
GetCount_management,
MakeFeatureLayer_management,
Project_management,
SelectLayerByLocation_management,
SetParameterAsText,
SetParameter,
SpatialReference
)
import os
import json
from ConfigParser import SafeConfigParser
class Toolbox(object):
def __init__(self):
"""Define the toolbox (the name of the toolbox is the name of the .pyt file)."""
self.label = "Toolbox"
self.alias = ""
# List of tool classes associated with this toolbox
self.tools = [Tool]
class Tool(object):
def __init__(self):
"""Define the tool (tool name is the name of the class)."""
self.label = "example_getParcelPolygons"
self.description = ""
# self.canRunInBackground = False
self.canRunInBackground = True
def getParameterInfo(self):
params = []
"""Define parameter definitions"""
param0 = arcpy.Parameter (
displayName="Input a geometry",
name="clip_geometry",
datatype="GPFeatureRecordSetLayer",
parameterType="Required",
direction="Input"
)
# Use __file__ attribute to find the .lyr file (assuming the
# .pyt and .lyr files exist in the same folder)
# example using a shared network path
param0.value = os.path.join("\\\\FILESTORE/GisLibrary/Scripts/your_folder",
"template.gdb/polygon")
param1 = arcpy.Parameter (
displayName="Parcel Boundary",
name="parcelBoundary",
datatype="DEFeatureClass",
parameterType="Derived",
direction="Output"
)
params.append(param0)
params.append(param1)
return params
def isLicensed(self):
"""Set whether tool is licensed to execute."""
return True
def updateParameters(self, parameters):
"""Modify the values and properties of parameters before internal
validation is performed. This method is called whenever a parameter
has been changed."""
return
def updateMessages(self, parameters):
"""Modify the messages created by internal validation for each tool
parameter. This method is called after internal validation."""
return
def execute(self, parameters, messages):
"""The source code of the tool."""
clip_geometry = parameters[0].valueAsText
parcelBoundary = parameters[1]
full_sde_conn = "\\\\FILESTORE/GisLibrary/DataConnections/Users/GISADMIN/your_sde_store_gisadmin.sde"
featureClass = "basemap.GISADMIN.Parcels_EXAMPLE".encode('utf-8')
xy_tolerance = ""
parcel_polys = os.path.join("in_memory", "clipped_parcels")
env.workspace = full_sde_conn
env.overwriteOutput = True
# clip_geometry = GetParameter(0)
# enable the below to get messages while testing, disable before publishing to AGS server
# AddMessage("Worspace: {0}".format(env.workspace))
# AddMessage("feature class: {0}".format(featureClass))
MakeFeatureLayer_management(featureClass, 'parcels_layer')
# here we select the parcels using the incoming geometry (will be a polygon from client )
parcels = SelectLayerByLocation_management ("parcels_layer", "INTERSECT", clip_geometry)
# how many parcels did we get?
# matchcount = int(GetCount_management('parcels_layer')[0])
# AddMessage("Count is: {0}".format(matchcount))
# our target feature_class
dissolved = "in_memory\dissolved_parcels"
Dissolve_management('parcels_layer', dissolved)
# matchcount1 = int(GetCount_management(dissolved)[0])
# AddMessage("Second Count is: {0}".format(matchcount1))
# now we reproject to web map sr to match the wab map
mapSR = SpatialReference(102100)
projected = "%scratchworkspace%\projected_parcels"
Project_management(dissolved, projected, mapSR)
# matchcount2 = int(GetCount_management(projected)[0])
# AddMessage("Third Count is: {0}".format(matchcount2))
# Set output parameter to return the data to the caller
SetParameter(1, projected)
AddMessage("parameter for out is {0}".format(parameters[1].valueAsText))
# cleanup workspace
Delete_management('parcels_layer')
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment