Skip to content

Instantly share code, notes, and snippets.

View NPS-ARCN-CAKN's full-sized avatar

NPS Arctic and Central Alaska Inventory and Monitoring Networks NPS-ARCN-CAKN

View GitHub Profile
@NPS-ARCN-CAKN
NPS-ARCN-CAKN / VB-LoopCSV.vb
Last active August 29, 2015 14:18
VB: Loop through a list of comma separated values
Dim MyString As String = "Fish, Cat, Dog"
Dim MyList() As String = MyString.Split(",")
For Each Item As String In MyList
MsgBox(Item)
Next
@NPS-ARCN-CAKN
NPS-ARCN-CAKN / OpenShapefile.py
Created April 13, 2015 20:44
Python: Open a shapefile and loop through the records
# basic example of opening a shapefile and then looping through the records and outputting them to console
import arcpy
# Supply a path to the shapefile
fc = "C:/Temp/MurphyProjected.shp"
# we'll need to create a searchcursor a little further on to access the records in the layer. the cursor has a fields parameter
# we could just submit a * to gather all columns except that we need the Shape column returned as a token, e.g. Shape@,
# so we have to submit all the columns as a list with Shape changed to the Shape@ token that will allow us to get at geometry info.
# The easiest way to do this is to loop through the column names and load them into a list making our edits as needed
@NPS-ARCN-CAKN
NPS-ARCN-CAKN / PythonLoopFeatureClassColumnNames.py
Last active August 29, 2015 14:19
Python: Loop through a FeatureClass' column names
import arcpy
fc = "C:/Work/VitalSigns/ARCN-CAKN Dall Sheep/Data/LegacySurveyUnits/ARCN_Subunits_Sheep_WGS84.shp"
fieldsList = arcpy.ListFields(fc) #get the fields
fields = [] # create an empty list
# loop through the fields and add the columns to the list, change the Shape column (containing geometry) into a token,
for field in fieldsList:
if field.name == "Shape":
fields.append("Shape@")
else:
fields.append(field.name)
@NPS-ARCN-CAKN
NPS-ARCN-CAKN / SelectPointsWithinPolygon.sql
Last active August 29, 2015 14:19
SQL: Select points that fall within a polygon
-- SQL Server 2008R2, there are better functions in later versions of Sql Server such as STContains()
SELECT Animals.GroupNumber
FROM LegacyUnits INNER JOIN
Animals ON LegacyUnits.PolygonFeature.STIntersects(Animals.Location) = 1
-- Here is an UPDATE query example:
UPDATE Animals
SET Unit = LegacyUnits.LegacyUnitID
FROM LegacyUnits INNER JOIN
Animals ON LegacyUnits.PolygonFeature.STIntersects(Animals.Location) = 1
@NPS-ARCN-CAKN
NPS-ARCN-CAKN / OpenFileDialogExample.vb
Created April 30, 2015 13:03
VB .NET: An example OpenFileDialog function
Private Function OpenFile() As String
'build and configure an OpenFileDialog
Dim OFD As New OpenFileDialog
With OFD
.AddExtension = True
.CheckFileExists = True
.Filter = "Excel files(.xlsx)|*.xlsx|Excel files (*.xls)|*.xls"
.Multiselect = False
.Title = "Select an workbook to open"
End With
@NPS-ARCN-CAKN
NPS-ARCN-CAKN / unpivot.sql
Created May 26, 2015 19:21
Example of an UNPIVOT query
use ARCN_Sheep
SELECT Survey, LegacyUnitID, Category, n
FROM [CompositionCountsByUnit]UNPIVOT (n FOR Category IN (Ewes, EweLike, Lambs, Rams_LessThanFullCurl, Rams_FullCurl, UnclassifiedRams, UnclassifiedSheep,
Yearlings, Rams1_2Curl, Rams3_4Curl, Rams7_8Curl, Rams1_4Curl, Rams_GT_7_8Curl)) u;
@NPS-ARCN-CAKN
NPS-ARCN-CAKN / ConnectSQLServerExecuteQueries.py
Last active August 29, 2015 14:22
Python: Connect to SQL Server database and execute queries
import pyodbc
connection = pyodbc.connect('DRIVER={SQL Server};SERVER=MYSQLSERVER\SERVERNAME;DATABASE=MyDatabase')
cursor = connection.cursor()
# select query example
cursor.execute("SELECT * FROM MyTable")
row = cursor.fetchone()
if row:
print row
@NPS-ARCN-CAKN
NPS-ARCN-CAKN / PythonExceptionExample.py
Created June 23, 2015 18:19
Python exception example
connection = pyodbc.connect('DRIVER={SQL Server};SERVER=' + server + ';DATABASE=' + database, autocommit = False)
cursor = connection.cursor()
try:
cursor.execute("INSERT INTO MyTable(a,b) VALUES('a','b')")
except Exception as ex:
print 'Error: ' + str(ex)
@NPS-ARCN-CAKN
NPS-ARCN-CAKN / ChangeFilename10KIterations.py
Created June 25, 2015 22:34
Change a filename every 10,000 iterations
for n in range(1,20010):
if not n % 10000:
filename = "ImportGPSPointsLog_" + str(n/10000) + ".sql"
print str(n) + ' ' + filename
@NPS-ARCN-CAKN
NPS-ARCN-CAKN / GetWKTRepresentationsOfFeatures
Last active January 6, 2016 19:32
Get WKT representations of features
import arcpy
# Note: Sometimes Shape@ is SHAPE@ which can mess things up
# one way
InFile = r'c:\temp\z144726127333.shp'
with arcpy.da.SearchCursor(InFile,'SHAPE@') as Cursor:
for row in Cursor:
print row[0].WKT
# another way because sometimes the first way doesn't work. This version will select two columns.