Skip to content

Instantly share code, notes, and snippets.

View AlexArcPy's full-sized avatar

Alexey Tereshenkov AlexArcPy

View GitHub Profile
@AlexArcPy
AlexArcPy / export_attachments_custom_name.py
Last active August 29, 2023 04:22
ArcPy: Batch export of attachments from geodatabase feature class with custom naming
'''This code will export the attachments associated with a feature class features into
a folder naming output files using the feature's attribute provided'''
import arcpy
import os
##define input parameters for the tool
#input attachments table
in_table = arcpy.GetParameterAsText(0)
#output folder to save files in
file_location = arcpy.GetParameterAsText(1)
@AlexArcPy
AlexArcPy / route_stops_distance.py
Created April 22, 2017 10:54
Arcpy: Get route distance and travel time between stops in an ArcMap Network Analysis layer
'''This is an ArcGIS Python add-in class for calculating the distance and the time
between the two selected by user Route network analysis layer stops. This code is
supposed to be wrapped into a Python add-in, but can also be executed as a standalone
script tool with minor modifications. This code assumes that the route has been
already solved and there are accumulators associated with the route solver'''
import arcpy
import arcpy.mapping as mp
import pythonaddins
arcpy.env.overwriteOutput = True
@AlexArcPy
AlexArcPy / find_diff.py
Last active April 23, 2017 19:14
ArcPy: Find geoprocessing tools difference between ArcGIS Pro and ArcGIS Desktop installations
'''Find difference in gp tools between Pro and Desktop'''
import json
pro_tools = dict(json.load(open(r"C:\GIS\Temp\Pro_tools.json")))
desktop_tools = dict(json.load(open(r"C:\GIS\Temp\Desktop_tools.json")))
#to get tools present in Pro but not in Desktop
for tbx, tools in pro_tools.iteritems():
print(tbx)
for i in [str(tool) for tool in tools if tool not in desktop_tools.get(tbx,[])]:
print(i)
@AlexArcPy
AlexArcPy / ProClassLibrary.cs
Created April 23, 2017 20:21
Calling ArcGIS Pro class library from Python
using System;
using ArcGIS.Core.Hosting;
using ArcGIS.Core.Data;
namespace SimplePro
{
public class DataManagement
{
public static string GetObjectIDField(string path, string shapefile)
{
@AlexArcPy
AlexArcPy / k_alternate_path.py
Created April 27, 2017 19:10
Network Analyst and ArcPy: finding k-alternate path
'''Python add-in that will find multiple k-alternate paths between a given number of stops.
This code is supposed to be run in ArcMap map document with a Route Network Analysis layer
named Route with at least two stops loaded. The impedance attribute name is read from a
Route solver properties object.'''
import os
import arcpy
import arcpy.mapping as mp
import pythonaddins
@AlexArcPy
AlexArcPy / create_fk.sql
Last active July 10, 2017 18:21
Geodatabase domains vs SQL constraints
UPDATE DBO.CITIES SET TYPE = 0 WHERE TYPE IS NULL
ALTER TABLE DBO.CITIES
ALTER COLUMN TYPE NVARCHAR(25) NOT NULL
--create FK for feature class Cities and store legal values for the attribute Type in the table CityType
ALTER TABLE DBO.CITIES DROP CONSTRAINT FK_CITYTYPES
DROP TABLE CityType
GO
@AlexArcPy
AlexArcPy / shp2geojson.py
Created July 28, 2017 09:52
Convert shapefile to GeoJSON with ogr and Python
import json
import ogr
driver = ogr.GetDriverByName('ESRI Shapefile')
shp_path = r'C:\GIS\Temp\Counties.shp'
data_source = driver.Open(shp_path, 0)
fc = {
'type': 'FeatureCollection',
'features': []
@AlexArcPy
AlexArcPy / gdb2elastic.py
Last active April 16, 2024 21:59
Convert geodatabase feature class features into GeoJSON strings for loading into elastic search database (bulk api)
'''
Convert file geodatabase feature class features into GeoJSON and
then construct text file for loading into elastic search bulk api
'''
import json
import arcpy
arcpy.env.overwriteOutput = True
#create a geojson file
@AlexArcPy
AlexArcPy / route2video.py
Created August 7, 2017 10:45
Generate a video street view stream based on a route polyline feature class using Mapillary API
'''
* generate a list of (x,y) for points along the route polyline feature class
with the set interval (using arcpy);
* save image from the API for each point trying to find the closest one as well
as the one looking at the next point in the route (using requests);
* merge the images into an output avi video file (using opencv);
'''
import os
import json
@AlexArcPy
AlexArcPy / 1read_gdb_domains_ogr.py
Last active December 10, 2025 01:24
Read file geodatabase domains with OGR without using arcpy and find what fields have assigned domains
from __future__ import print_function
import json
import xml.etree.ElementTree as ET
import ogr
gdb_path = r'C:\GIS\data\Adv.gdb'
ds = ogr.Open(gdb_path)
res = ds.ExecuteSQL('select * from GDB_Items')
res.CommitTransaction()