Skip to content

Instantly share code, notes, and snippets.

@ebrelsford
ebrelsford / forms.py
Created March 28, 2013 13:56
A modification of Django's admin widget RelatedFieldWidgetWrapper, which adds the handy + to select boxes when picking a related model. This one doesn't assume the user's picking a related model instance--it simply lets the user pick a model instance or add a new one if needed. This is useful on forms (such as the example in forms.py) where you …
"""
An example form using AddAnotherWidgetWrapper.
"""
from django import forms
from .models import Owner
from .widgets import AddAnotherWidgetWrapper
class PickOwnerForm(forms.Form):
@ebrelsford
ebrelsford / Leaflet.geojsonbounds.js
Created March 28, 2013 21:11
A mixin for L.LatLngBounds that adds a toGeoJson method.
L.extend(L.LatLngBounds.prototype, {
toGeoJson: function() {
return {
'type': 'Polygon',
'coordinates': [[
[this.getSouthWest().lng, this.getSouthWest().lat],
[this.getNorthWest().lng, this.getNorthWest().lat],
[this.getNorthEast().lng, this.getNorthEast().lat],
[this.getSouthEast().lng, this.getSouthEast().lat],
@ebrelsford
ebrelsford / reversion_utils.py
Created April 1, 2013 16:50
A mixin for Django model managers that adds an initial revision using django-reversion when the object is created via get_or_create().
from django.utils.importlib import import_module
from reversion import default_revision_manager
class InitialRevisionManagerMixin(object):
def _save_initial_revision(self, obj):
# try to load admin
try:
@ebrelsford
ebrelsford / gist:5592416
Created May 16, 2013 15:07
Get clusters from a GeoDjango model that could be used in a heatmap (eg, using Heatmap.js), including potentially dynamic filters on the model.
from django.db import connection, DEFAULT_DB_ALIAS
num_clusters = 6
queryset = model.objects.filter() # Your filters here
queryset = queryset.filter(centroid__isnull=False).only('centroid')
# Get the query for
sql_model_query, params = queryset.query.get_compiler(DEFAULT_DB_ALIAS).as_sql()
@ebrelsford
ebrelsford / nycproj2latlon.sql
Last active December 21, 2015 05:39
Convert a table with coordinates in NAD_1983_StatePlane_New_York_Long_Island_FIPS_3104_Feet (common for NYC data) to lat and lon in 4326. We first convert the coordinates from feet to meters.
UPDATE graffiti_locations
SET the_geom = ST_Transform(
ST_SetSRID(
ST_MakePoint(x * 0.3048006096012192, y * 0.3048006096012192),
32118),
4326
)
@ebrelsford
ebrelsford / neontext.css
Created August 27, 2013 01:17
CartoCSS for a neon sign effect as used in http://cdb.io/15ekzYE
@occurrences0inner: #5EF2F4;
@occurrences0outer: #272BB3;
@occurrences1inner: #FAFEFE;
@occurrences1outer: #07A40B;
@occurrences2inner: #FFE721;
@occurrences2outer: #95521E;
@occurrences3inner: #EFFC40;
@ebrelsford
ebrelsford / signcount.sql
Created August 27, 2013 01:25
Get the number of times the text on a sign occurs, as used here: http://cdb.io/15ekzYE
UPDATE sign_application_filings s1
SET text_occurrences = (
SELECT COUNT(DISTINCT(s2.house, s2.street_name))
FROM sign_application_filings s2
WHERE s2.text_on_sign = s1.text_on_sign
)
WHERE text_on_sign != ''
@ebrelsford
ebrelsford / signselect.sql
Created August 27, 2013 01:26
Select unique sign occurrences as used here: http://cdb.io/15ekzYE
SELECT
house,
street_name,
MIN(cartodb_id) AS cartodb_id,
MIN(the_geom) AS the_geom,
MIN(the_geom_webmercator) AS the_geom_webmercator,
MIN(text_on_sign) AS text_on_sign,
MIN(text_occurrences) AS text_occurrences,
MIN(rotation) AS rotation
FROM sign_application_filings
@ebrelsford
ebrelsford / citibike.geojson
Created September 7, 2013 19:49
NYC citibike stations snapshot
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@ebrelsford
ebrelsford / index.html
Last active December 30, 2015 23:29
Trying to embed a MapBox map with a legend.
<!doctype html>
<html>
<body>
<iframe width='100%' height='500px' frameBorder='0' src='https://a.tiles.mapbox.com/v3/ebrelsford.gghlfpek/mm/zoompan,zoomwheel.html?secure=1#12/30.0273/-90.0156'></iframe>
</body>
</html>