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 / 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 / AccessFeatureClassData.py
Last active December 17, 2015 19:29
ArcPy: Access feature class data
import arcpy
import numpy
input = r"C:\Work\Waypoints.shp"
arr = arcpy.da.FeatureClassToNumPyArray(input, ('ident', 'Latitude', 'Longitude'))
for row in arr:
ident = row["ident"]
lat = row["Latitude"]
lon = row["Longitude"]
sql = "INSERT INTO MyTable(WaypointName,Lat,Lon) VALUES('" + str(ident) + "'," + str(lat) + "," + str(lon) + ");"
print sql
@NPS-ARCN-CAKN
NPS-ARCN-CAKN / ToggleFormEditability.vb
Last active December 21, 2015 16:28
Access: Toggle a form's editability (Read only/Editable)
' this button allows the user to switch between read only and editing
Private Sub ToggleReadOnlyButton_Click()
SetFormEditability
End Sub
' you can only toggle the form's allowedits property is it is clean so if the
' form is dirty then ask if it's ok to flush changes
Private Sub SetFormEditability()
If Me.Dirty = True Then
If MsgBox("Save changes?", vbYesNo, "Confirm") = vbYes Then