Skip to content

Instantly share code, notes, and snippets.

Avatar

Bryan McIntosh TheBryanMac

  • Ontario, Canada
View GitHub Profile
@TheBryanMac
TheBryanMac / ArcPy_SDE_and_Versions.py
Created Oct 1, 2018
Python ArcPy Connect to SDE and versions
View ArcPy_SDE_and_Versions.py
#Name: Python ArcPy Connect to SDE and versions
#Author: Bryan McIntosh
#Description: An approach to connect to SDE, create a new version, and change
# to the new version - inside a python script.
import arcpy, datetime, os, tempfile, shutil
#Create a string of the date to append to version name (to keep unique)
now = datetime.datetime.now()
strDate = str(now.year) + str(now.month) + str(now.day) + str(now.hour) + str(now.minute) + str(now.second)
#Create a temp directory using tempfile module to store SDE connection files
sdeTempPath = tempfile.mkdtemp()
@TheBryanMac
TheBryanMac / MultiField_to_SingleKey.py
Created Jan 4, 2018
Multiple Field Key to Single Field Key for Relates using Python and ArcPy
View MultiField_to_SingleKey.py
#Name: Multiple Field Key to Single Field Key for Relates using Python and ArcPy
#Author: Bryan McIntosh
#Description: For updating a single LINK_ID field in both the parent and child table
# based on a natural 4 column composite index relationship.
import arcpy
## VARIABLES ##
edit_ws = r'C:\SpatialTimes.gdb' #Edit workspace
xs_fc = r'C:\SpatialTimes.gdb\CrossSection' #parent FC
xs_tbl = r'C:\SpatialTimes.gdb\CrossSection_RatingCurves' #related table
xs_att = ['Link_ID','F1','F2','F3','F4','OID@']
@TheBryanMac
TheBryanMac / ArcMapLayersToLYRFiles.py
Created Oct 30, 2017
Iterate through an ArcMap MXD, and export all layers to LYR files.
View ArcMapLayersToLYRFiles.py
#Name: ArcMap Layers to LYR Files with Python
#Author: Bryan McIntosh
#Description: Iterate through an ArcMap MXD, and export all layers to LYR files.
import arcpy, os
#Set the working directory to where the Python file is stored
cwd = os.getcwd()
#Set the location of the MXD file
mxdPath = os.path.join(cwd,"MyMap.mxd")
#Set the location to store LYR files
@TheBryanMac
TheBryanMac / ArcGISServer_GenerateSecureToken.py
Created Aug 23, 2017
Python script to generate a token from ArcGIS Server for use with secure map services
View ArcGISServer_GenerateSecureToken.py
#Name: Generate a token from ArcGIS Server using python
#Author: Bryan McIntosh
#Description: Python script to generate a token from ArcGIS Server for use with secure map services.
# This sample uses the 'requests' module (https://pypi.python.org/pypi/requests/2.7.0)
import requests
params = {'username': 'USERNAME', 'password': 'PASSWORD', 'client': 'requestip', 'f': 'json'}
request = requests.post('https://YOUR.SITE.com/arcgis/tokens',params=params)
response = request.json()
myToken = response["token"]
@TheBryanMac
TheBryanMac / MapService2ShapefileIterate.py
Last active Nov 19, 2019
Python script that connects to an ArcGIS Server Map Service and downloads a single vector layer to shapefiles. If there are more features than AGS max allowed, it will iterate to extract all features.
View MapService2ShapefileIterate.py
#Name: Export ArcGIS Server Map Service Layer to Shapefile with Iterate
#Author: Bryan McIntosh
#Description: Python script that connects to an ArcGIS Server Map Service and downloads a single vector layer
# to shapefiles. If there are more features than AGS max allowed, it will iterate to extract all features.
import urllib2,json,os,arcpy,itertools
ws = os.getcwd() + os.sep
#Set connection to ArcGIS Server, map service, layer ID, and server max requests (1000 is AGS default if not known).
serviceURL = "https://domain/arcgis/rest/services"
@TheBryanMac
TheBryanMac / MapService2Shapefile.py
Last active Jul 28, 2018
Export ArcGIS Server Map Service Layer to Shapefile
View MapService2Shapefile.py
#Name: Export ArcGIS Server Map Service Layer to Shapefile
#Author: Bryan McIntosh
import urllib2, os, arcpy
# Variables
myUrl = "https://sampleserver6.arcgisonline.com/arcgis/rest/services"
myService = "/Census/MapServer"
myParams = "/3/query?where=1%3D1&geometryType=esriGeometryEnvelope&spatialRel=esriSpatialRelIntersects&relationParam=&outFields=*&returnGeometry=true&geometryPrecision=&outSR=&returnIdsOnly=false&returnCountOnly=false&orderByFields=&groupByFieldsForStatistics=&returnZ=false&returnM=false&returnDistinctValues=false&returnTrueCurves=false&f=pjson"
# Query ArcGIS Server Map Service
@TheBryanMac
TheBryanMac / STIntersection_PROC.sql
Created Sep 12, 2014
SQL Procedure with STIntersection
View STIntersection_PROC.sql
-- Name: SQL Server Procedure with STIntersection
-- Author: Bryan McIntosh
-- Description: Populated data table from SQL Server Spatial query
CREATE PROCEDURE [dbo].[prc_NoaaDataLoad]
@dt datetime, --DateTime from NOAA header info
@val float, --Value of weather data (mm of rain, temperature in F/C, etc)
@wType varchar(20), -- Type of data stored (precipitation, temperature, etc)
@wkt varchar(8000) --polygon shape (geography type)
AS
@TheBryanMac
TheBryanMac / SQL STIntersection.sql
Created Aug 14, 2014
SQL Server STIntersection Example
View SQL STIntersection.sql
-- Name: SQL Server STIntersection Example
-- Author: Bryan McIntosh
/****** Part 1: Intersect Point and Polygon ******/
DECLARE @thePolygon GEOGRAPHY, @pointIN GEOGRAPHY, @pointOUT GEOGRAPHY;
SET @thePolygon = GEOGRAPHY::STGeomFromText('POLYGON((-78.50932668617881 45.024933647425115, -78.53403351361905 44.9898648154388, -78.48446979547693 44.97239241709962, -78.45973073293072 45.007441690111115, -78.50932668617881 45.024933647425115))', 4269);
SET @pointIN = GEOGRAPHY::STGeomFromText('POINT(-78.51 45.00)', 4269);
SET @pointOUT = GEOGRAPHY::STGeomFromText('POINT(-65.00 35.00)', 4269);
SELECT @pointIN.STIntersection(@thePolygon).ToString();
SELECT @pointOUT.STIntersection(@thePolygon).ToString();
@TheBryanMac
TheBryanMac / BitFlagFunctions.py
Last active Aug 29, 2015
BitCode/BitFlag Python Example
View BitFlagFunctions.py
# Name: BitCode/BitFlag Python Example
# Author: Bryan McIntosh
######################################################################
# Set bitCode/Flag to run various/multiple sections of code.
# Add the numbers together for each section to run.
# 1: Call section 0 - example: Clean temp directory
# 2: Call section 1 - example: Run FTP Download
# 4: Call section 2 - example: Analyze FTP files and clean
# 8: Call section 3 - example: Say hi
######################################################################
@TheBryanMac
TheBryanMac / fGDB2SQL.py
Last active Aug 29, 2015
Feature Class export into native SQL Server table
View fGDB2SQL.py
#Requires: Esri's ArcPy.da module and PyODBC module
#Note: Search for <VARIABLE> to replace with new values, and update inputs
import arcpy, pyodbc
def fn_ReadPolygons(fc_in,wkid):
#This function loops through a polygon layer and converts geometry to WKT format
#Additional attributes can also be added for insert
#The input layer and WKID of the Geography type is required
for row in arcpy.da.SearchCursor(fc_in, ["SHAPE@WKT"]):
You can’t perform that action at this time.