Skip to content

Instantly share code, notes, and snippets.

View AlexArcPy's full-sized avatar

Alexey Tereshenkov AlexArcPy

View GitHub Profile
@AlexArcPy
AlexArcPy / drop_db_with_pattern.sh
Created May 28, 2019 22:09
Drop PostgreSQL database matching name pattern
echo "Drop PostgreSQL database matching a pattern"
cd C:/Temp
sql_query="select 'drop database \"'||datname||'\";' from pg_database WHERE datistemplate = false and datname like 'operat%';"
echo $sql_query
PGPASSWORD=postgres psql -p 5432 -d postgres -U postgres -t -A -F"," -c "${sql_query}" > drop_dbs.sql
cat drop_dbs.sql | while read line
do
echo $line;
PGPASSWORD=postgres psql -p 5432 -d postgres -U postgres -t -A -F"," -c "${line}"
@AlexArcPy
AlexArcPy / ogrtools.sh
Created May 28, 2019 21:56
Using bash to run OGR command line tools
echo "Working directory: $PWD";
echo "List files in a given directory"
for file in C:/GISData/*; do
echo $file;
done
echo "Get basic info about all shapefiles in a given folder"
for file in C:/GISData/*; do
if [[ ${file: -4} == ".shp" ]]
# flake8 > C:/Temp/StyleGuide.csv
import os
os.chdir('C:/Temp')
import pandas as pd
for errors_file in ['StyleGuide1.csv', 'StyleGuide2.csv']:
df = pd.read_csv(errors_file, sep=':')
df.columns = ['FilePath', 'LineNo', 'ColumnNo', 'ErrorMessage']
@AlexArcPy
AlexArcPy / code_for_flake8_to_analyze.py
Created January 8, 2019 20:50
Exploring flake8 api
def some():
print()
@AlexArcPy
AlexArcPy / preferences
Created June 2, 2018 09:28
Wing IDE preferences to reuse between installations.
# To be put into "C:\Users\alte\AppData\Roaming\Wing IDE 6\preferences"
[user-preferences]
cache.external-check-freq = 0.1
consoles.auto-clear = True
consoles.wrap-long-lines = True
debug.multi-process-debug-sub-processes = True
debug.pretty-print-in-shells = True
debug.prompt-to-restart-python-shell-debug = False
debug.show-breaks-moved-message = False
debug.wrap-debug-io = True
@AlexArcPy
AlexArcPy / heapq_merge.py
Created April 3, 2018 09:07
Demonstrating efficient use of heapq.merge when working with arcpy.da.SearchCursor structures
'''
Demonstrating efficient use of heapq.merge when working with
arcpy.da.SearchCursor structures. The heapq.merge function will
merge multiple sorted inputs into a single sorted output which
can be useful when iterating over multiple arcpy.da.SearchCursor
iterators.
'''
from pprint import pprint
import heapq
@AlexArcPy
AlexArcPy / pprint_table_arcpy.py
Created March 24, 2018 07:54
Printing pretty tables with Python in ArcGIS
# using the built-in Python modules
import arcpy
arcpy.env.workspace = r'C:\Program Files (x86)\ArcGIS\Desktop10.5\TemplateData\TemplateData.gdb'
fc = r'USA\cities'
data = ['CITY_FIPS', 'CITY_NAME', 'STATE_FIPS', 'STATE_NAME', 'POP1990']
feats = [f for f in arcpy.da.SearchCursor(fc, field_names=data)][:10]
arcpy.AddMessage(" ")
row_format = u"{:>20}" * (len(data))
arcpy.AddMessage(row_format.format(*data))
@AlexArcPy
AlexArcPy / MultipleRingBufferPostGIS.sql
Created March 23, 2018 10:43
Multiple Ring Buffer with PostGIS
--using set returning function generate_series
--https://www.postgresql.org/docs/9.1/static/functions-srf.html
WITH buffer_values as (
SELECT * FROM generate_series(5, 20, 5) AS dist
)
SELECT
CAST(p.id AS INT), p.name, b.dist, ST_Buffer(p.geom, b.dist) AS Poly
FROM
nyc_subway_stations p
@AlexArcPy
AlexArcPy / MultipleRingBufferSS.sql
Created March 23, 2018 10:38
Multiple Ring Buffer with SQL Server
--simple list of values to generate buffers around
DECLARE @buffer_distances TABLE (dist int);
INSERT @buffer_distances(dist) VALUES (1), (2), (3);
SELECT
p.STATE_NAME, p.CITY_NAME, b.dist, p.Shape.STBuffer(b.dist) AS Poly
FROM
CITIES p
CROSS JOIN
@buffer_distances b
@AlexArcPy
AlexArcPy / python_startup.py
Created March 15, 2018 12:07
Python start up script for ArcGIS Desktop users
'''
Python start up file that supports both Python 2 and 3.
Helpful set of preimported modules and predefined
variables for ArcMap and ArcGIS Pro users.
'''
# pretty printing instead of regular print
# https://docs.python.org/2/library/pprint.html
from pprint import pprint
import os