Skip to content

Instantly share code, notes, and snippets.

View AlexArcPy's full-sized avatar

Alexey Tereshenkov AlexArcPy

View GitHub Profile
@AlexArcPy
AlexArcPy / arcpy_to_from_wkt.py
Created March 14, 2018 16:37
ArcPy to WKT and from WKT
import arcpy
# read California cities features' geometries into a WKT string
arcpy.Multipoint(
arcpy.Array([
arcpy.Point(*coords) for coords in [
r[0] for r in arcpy.da.SearchCursor('cities', 'SHAPE@XY',
"STATE_NAME = 'California'",
arcpy.SpatialReference(3857))
]
@AlexArcPy
AlexArcPy / use_custom_selection_color_arcpy.py
Created March 6, 2018 15:01
Changing selection color in exporting ArcMap layout with ArcPy
'''
Script that demonstrates how to use predefined layer files (.lyr)
to change the selection symbol for any map document layer. The
selection symbol is not exposed via `arcpy.mapping.Layer` object, so
it is necessary to emulate the selection using another layer drawn
on top of the layer that you targeting your selection against.
The script assumes that you have prepared beforehand 3 layers (.lyr
files) for each shape type (point, polyline, polygon).
'''
@AlexArcPy
AlexArcPy / sql2pandas2sql.ipynb
Created February 22, 2018 10:39
Using IPython SQL magic in a Jupyter notebook to create new database tables using the PERSIST command
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@AlexArcPy
AlexArcPy / sql_server_spatial_functions_p2.ipynb
Created February 17, 2018 07:24
SQL Server spatial functions for GIS users: part 2
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@AlexArcPy
AlexArcPy / sqlmagic.ipynb
Created February 8, 2018 09:46
Adding IPython SQL magic to Jupyter notebook
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@AlexArcPy
AlexArcPy / concave_hulls_pyqt.py
Created November 27, 2017 17:23
Interactive PyQt app: generate alpha shapes (concave hulls) using Matplotlib, shapely, and arcpy inside ArcGIS Pro
'''
To be able to run this tool inside ArcGIS Pro:
1. Copy the ArcGIS Pro env to a new folder.
2. In clean ArcGIS Pro env, install with built-in Pro package manager:
* PyQt 5.6
* shapely 1.7
3. Create a new custom script tool (with a parameter to specify path to a shapefile
or a feature class) and point to this source Python module.
4. Run the script tool. It might take some seconds for the tool to start. This is
@AlexArcPy
AlexArcPy / arcgispro_basic_app_threading.py
Created November 25, 2017 16:04
Basic PyQt5 application to run in ArcGIS Pro custom script tool: running in own thread
import sys
import threading
import arcpy
from PyQt5.QtWidgets import (QMainWindow, QLabel, QApplication)
from PyQt5.QtCore import Qt
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
@AlexArcPy
AlexArcPy / arcgispro_basic_app.py
Last active November 25, 2017 16:04
Basic PyQt5 application to run in ArcGIS Pro custom script tool: running in the thread of Pro
import sys
import arcpy
from PyQt5.QtWidgets import (QMainWindow, QLabel, QApplication)
from PyQt5.QtCore import Qt
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.setWindowTitle("Basic App")
@AlexArcPy
AlexArcPy / remove_redundant_vertices.py
Created November 8, 2017 10:14
Remove redundant vertices in polyline features using arcpy
import arcpy
#----------------------------------------------------------------------
def are_collinear(p1, p2, p3, tolerance=0.5):
"""return True if 3 points are collinear.
tolerance value will decide whether lines are collinear; may need
to adjust it based on the XY tolerance value used for feature class"""
x1, y1 = p1[0], p1[1]
x2, y2 = p2[0], p2[1]
@AlexArcPy
AlexArcPy / create_mxd_comtypes.py
Created November 5, 2017 09:08
Create a new empty ArcGIS map document (.mxd) with Python (comtypes)
from comtypes.client import GetModule, CreateObject
from snippets102 import GetStandaloneModules, InitStandalone
esriCarto = GetModule(r"C:\Program Files (x86)\ArcGIS\Desktop10.5\com\esriCarto.olb")
# First time through, need to import the “StandaloneModules”. Can comment out later.
GetStandaloneModules()
InitStandalone()
# Create a map document object