Skip to content

Instantly share code, notes, and snippets.

View maptastik's full-sized avatar

Ryan Cooper maptastik

View GitHub Profile
@maptastik
maptastik / randomFCSample.py
Last active November 26, 2018 14:02
Create a random sample from a dataset based on OBJECTID...not perfect, but a good start!
import arcpy
import numpy as np
def randomFCSample(fc, fd='sample_fd', sample_field='OBJECTID', sample_pct=10):
count_class = arcpy.GetCount_management(fc)
count = int(count_class[0])
random_vals = np.random.choice(count, int(count*(sample_pct/100)))
arcpy.MakeFeatureLayer_management(fc,
fd + "_" + str(sample_pct) + 'pct',
sample_field + ' IN ' + str(tuple(random_vals)))
@maptastik
maptastik / test_track.geojson
Last active November 26, 2018 14:46
A single GPS track of a cycling trip as GeoJSON
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@maptastik
maptastik / TEMPLATE.mapx
Created January 24, 2018 20:02
ArcGIS Pro map template.
{
"type" : "CIMMapDocument",
"version" : "2.0.0",
"build" : 8933,
"mapDefinition" : {
"type" : "CIMMap",
"name" : "TEMPLATE",
"uRI" : "CIMPATH=map6/map6.xml",
"sourceModifiedTime" : {
"type" : "TimeInstant"

I installed QGIS (KyngChaos) after installing Miniconda. When I would open QGIS, I'd get an error about MetaSearch not started because requests couldn't be found. How I got things working again

Alias system Python 2.7 (This may be optional)

sudo alias python2=/usr/bin/python2.7

Get a copy of get-pip.py and use it to install pip

sudo /usr/bin/python2.7 /Users/maptastik/get-pip.py

@maptastik
maptastik / ogrIntoPostGIS.md
Last active March 15, 2024 14:27
Import data into PostGIS with ogr2ogr#

Generally I can use QGIS and its DBManager to import data into a PostGIS database. Sometimes that doesn't work perfectly. ogr2ogr can help though. Here are a few approaches to getting data into PostGIS with ogr2ogr.

This is probably the most basic approach:

 ogr2ogr -f "PostgreSQL" PG:"dbname=<db name> user=<username> password=<password> host=<host> port=<port #>" input.geojson -nln schema.table

Running this little command seems to work if you have need to specify the geometry type (Source):

@maptastik
maptastik / UsefulSQL.md
Last active January 12, 2018 14:29
A collection of useful SQL queries

Get a list of duplicate records and how many duplicates there of that record

SELECT DISTINCT SOME_FIELD, count(*)
FROM SOME_TABLE
GROUP BY SOME_FIELD
HAVING count(*) > 1

Spatial join based that adds polygon values (b) to intersecting point features (a). Be aware that this does not return a value for those points that are outside a polygon feature

@maptastik
maptastik / agolToDF.py
Last active December 11, 2017 18:28
Search, select, and generate a pandas DataFrame from a Feature Layer Collection on ArcGIS Online.
import pandas as pd
from arcgis.gis import GIS
# Login to AGOL
gis = GIS()
# For other login methods see: https://developers.arcgis.com/python/guide/working-with-different-authentication-schemes/
# use for logging in within active ArcGIS Pro session
# gis = GIS('pro')
@maptastik
maptastik / IntroToSQLForDataScience_Notes.md
Last active January 1, 2019 23:56
Notes from DataCamp's "Intro to SQL for Data Science" course

Get a list of distinct values for a column in a table

SELECT DISTINCT column FROM table;

Get the count of rows in a table

SELECT COUNT(*) FROM table;
@maptastik
maptastik / nullVsZeroShp.py
Last active November 26, 2018 14:54
A little function for use in ArcGIS to determine in a very particular case if a field is a 0 or a null. Turns null into -1 because shapefiles don't do null values. This is poorly documented...grr.
def calc(main_code, field_list):
if sum(field_list) == 0:
return -1
else:
return main_code
@maptastik
maptastik / geocode.ipynb
Last active November 26, 2018 14:56
A notebook to demonstrate how to use pandas and geopy to geocode a CSV of summer camp locations
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.