Skip to content

Instantly share code, notes, and snippets.

View phargogh's full-sized avatar

James Douglass phargogh

View GitHub Profile
@phargogh
phargogh / union.py
Last active August 29, 2015 14:17
Failing function to take union of polygons and write to a vector
"""Module for preprocessing operations."""
# Including all import statements from the original file, in case that matters
# Original file available (raw) at http://adept.invest-natcap.googlecode.com/hg/adept/preprocessing.py
import os
import logging
import tempfile
import shutil
import math
import json
@phargogh
phargogh / 0_reuse_code.js
Last active September 3, 2015 22:48
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@phargogh
phargogh / FOO.bar
Created October 24, 2019 22:15
This is a test gist
This a file in a gist.
@phargogh
phargogh / landcover-raster-attributes.py
Created December 17, 2019 19:55
A function to get pixel values from a GDAL landcover raster
import collections
from osgeo import gdal
RasterStats = collections.namedtuple('RasterStats', ['min', 'max'])
def get_raster_values_from_attr_table(filepath, band_index):
gdal_dataset = gdal.OpenEx(filepath, gdal.OF_RASTER)
band = gdal_dataset.GetRasterBand(1)
attr_table = band.GetDefaultRAT()
@phargogh
phargogh / README.txt
Created February 5, 2020 02:53
Running a python program in GDB
To run a python program (such as nosetests) in GDB, do the following:
$ gdb --args python -m nose
Then at the gdb shell:
> start
> continue
@phargogh
phargogh / fix_geometry.py
Created February 5, 2020 03:10
Demo: Fixing invalid geometries
# encoding=UTF-8
"""fix_geometry.py"""
def doit(original_geometry):
if not original_geometry.IsValid():
if 'POLYGON' in original_geometry.GetGeometryName():
# Geometries that are only self-intersecting can be fixed by
# Buffering by 0.
buffered_geom = original_geometry.Buffer(0)
@phargogh
phargogh / diffgdalinfo.sh
Created February 5, 2020 03:11
Compare the output of two gdalinfo calls via diff
#! /bin/bash
diff <(gdalinfo $1) <(gdalinfo $2)
@phargogh
phargogh / README.txt
Created February 5, 2020 03:13
Create an aspatial geopackage table
In the latest version of watershed delineation, a single watershed
seed may be associated with many features of interest. For this reason,
we need to be able to associate the attributes of the features of interest
with each of the seeds derived from the features.
The GeoPackage driver (https://www.gdal.org/drv_geopackage.html) supports
'aspatial' tables in GDAL>2.0, and 'attribute' data types in GDAL>2.2.
The two prototypes needed are:
* How do we make an aspatial table in GDAL 2?
@phargogh
phargogh / man2pdf.sh
Created February 5, 2020 03:16
Convert a manpage to PDF
#!/bin/bash
#
# Convert a program's manpage to PDF.
# Assumes that the program has a manpage for it and that ps2pdf14 is installed (part of a latex package).
#
# Example usage:
# $ ./man2pdf bash
${1?"Usage: $0 PROGRAMNAME"}
man -t $1 | ps2pdf14 - $1_manpage.pdf
@phargogh
phargogh / README.rst
Created February 11, 2020 02:33
Mercurial (hg) commands I usually need and regularly forget

Which files changed on my feature branch?:

hg status --rev .:develop

Which commits belong to a named branch?:

hg log -r "branch(<branchname>) and 0:"

What heads exist on the current repository that have not been merged into the develop branch?: