Skip to content

Instantly share code, notes, and snippets.

View AlexArcPy's full-sized avatar

Alexey Tereshenkov AlexArcPy

View GitHub Profile
@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 / 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 / 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 / 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 / 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 / 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 / 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 / create_convex_hull.py
Created April 18, 2017 10:53
ArcPy: Create Convex Hull polygons from features of various geometry type optionally grouping input features
import arcpy
arcpy.env.overwriteOutput = True
#----------------------------------------------------------------------
def create_convex_hull(in_fc, grouping_field=None):
"""
generate convex hull features optionally grouping input features;
if no grouping_field specified, a single Convex Hull will be generated
for all input features
"""
@AlexArcPy
AlexArcPy / edit_scale_bar_arcobjects.py
Created April 17, 2017 07:54
Modify properties of scale bar element in ArcMap map document layout using Python and ArcObjects
'''
This code provides access to a map document layout MapsurroundElement such as scale bar.
In order to use this code, you would need to have a map document with a pre-created
scale bar.
'''
import sys
from comtypes.client import GetModule, CreateObject
from snippets102 import GetStandaloneModules, InitStandalone
@AlexArcPy
AlexArcPy / mxd_map_grid_show_hide.py
Created April 14, 2017 16:11
Show or hide ArcMap map document (.mxd) map grids using comtypes and ArcObjects
'''
This code shows how to hide/show map grids in a map document before exporting the map.
This can be handy when you have multiple grids with different grid cell size and you
want to be able to control at what map scales each of the map grids should be visible.
As this is not exposed via arcpy, you have to use ArcObjects.
This code assumes there are two grids for the data frame with the name `small` and `large`
'''
import sys
from comtypes.client import GetModule, CreateObject