Skip to content

Instantly share code, notes, and snippets.

View capooti's full-sized avatar

Paolo Corti capooti

View GitHub Profile
@capooti
capooti / gist:2291849
Created April 3, 2012 13:06
Parse a KML file and export it to GeoJSON with GDAL OGR Python bindings
from osgeo import ogr
def kml2geojson(kml_file):
drv = ogr.GetDriverByName('KML')
kml_ds = drv.Open(kml_file)
for kml_lyr in kml_ds:
for feat in kml_lyr:
print feat.ExportToJson()
kml2geojson('/path/to/your.kml')
@capooti
capooti / gist:5245327
Created March 26, 2013 13:24
Sample Virtual Host for GeoNode with Apache
NameVirtualHost *:80
<VirtualHost *:80>
ServerName localhost
ServerAdmin me@example.org
# Log Files
LogLevel info
ErrorLog /var/log/apache2/error-geonode-2.log
CustomLog /var/log/apache2/access-geonode-2.log combined
DocumentRoot /home/capooti/git/github/capooti/geonode-master/geonode/static_root
Alias /static/ /home/capooti/git/github/capooti/geonode-master/geonode/static_root/
@capooti
capooti / gist:5245341
Created March 26, 2013 13:26
Sample wsgi script to be used in Apache for a Django application
ALLDIRS = ['/home/capooti/.venvs/geonode/lib/python2.7/site-packages']
import os
import sys
import site
# Remember original sys.path.
prev_sys_path = list(sys.path)
# Add each new site-packages directory.
@capooti
capooti / gist:97f2a0b2950c81711deb
Last active August 29, 2015 14:01
Create the most frequent value lists for a grouped field in a csv file with pandas
from pandas import read_csv
df = read_csv(open('zimbabwe.csv'))
columns = ('apr_09','jul_09','sep_09','jan_10','apr_10','jul_10','dec_10','apr_11','jul_11','sep_11','dec_11','mar_12','jul_12','Sep-12','Dec-12','feb_mar13','Jul-13','aug_sep13')
group_field = 'DISTRICT'
joined = df[['DISTRICTPC', group_field]].drop_duplicates()
for c in columns:
sdf = df[[group_field, c]]
result = sdf.groupby([group_field]).agg(lambda x:x.value_counts().index[0])
@capooti
capooti / gist:f1f272eeefc1ceb89007
Created August 20, 2014 10:03
Rename shapefile field names from uppercase to lowercase using OGR
declare -a fields=("ROADID" "SOURCEID" "NOTES" "ONME" "RTENME" "NTLCLASS" "FCLASS" "NUMLANES" "SRFTPE" "SRFCOND" "SRFPREP" "ISSEASONAL" "CURNTPRAC" "GNRALSPEED" "GDWTHRPRAC" "BDWTHRPRAC" "SHAPE_LENG" "ADM0_CODE" "ADM0_NAME" "CONTINENT" "REGION")
shp="was_trs_roads_wfp"
for f in "${fields[@]}"
do
lf=$(echo $f | awk '{print tolower($0)}')
echo $lf
ogrinfo $shp.shp -sql "ALTER TABLE $shp RENAME COLUMN $f TO $lf"
done
def add_user_in_registered_group(user):
from geonode.groups.models import GroupProfile
from geonode.people.models import Profile
try:
gp = GroupProfile.objects.get(slug='registered')
except GroupProfile.DoesNotExist:
gp = GroupProfile.objects.create(
title=_('Registered GeoNode Users'),
slug='registered',
description=_('Group containg all of the GeoNode users'),
@capooti
capooti / wfs_jsonp_request
Created November 25, 2015 15:56
WorldMap sample jsonp request
<html>
<head>
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet/v0.7.7/leaflet.css" />
<script src="http://cdn.leafletjs.com/leaflet/v0.7.7/leaflet.js"></script>
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
</head>
<body>
<div id="map" style="width: 800px; height: 600px;"></div>
<script type="text/javascript">
@capooti
capooti / fixmaps.py
Last active November 23, 2016 23:22
Fix old WorldMap maps
import json
from geonode.maps.models import Map
broken_maps = [2517,
2519,
2520,
2521,
2522,
2525,
2527,
from hypermap.aggregator.models import Layer
from hypermap.aggregator.tasks import index_cached_layers
from django.core.cache import cache
layers = Layer.objects.filter(service__srs__code=102100).filter(is_valid=True)
layers_cache = set([0])
for layer in layers:
layers_cache.add(layer.id)
cd /mydir
url="http://localhost:8080/geoserver/wms?STYLES=&FORMAT=image%2Fpng&TRANSPARENT=TRUE&LAYERS=geonode%3ADE01_SL6_22P_1T_20100521T010517_20100521T010602_DMI_0_1b5d_qXM&EXCEPTIONS=application%2Fvnd.ogc.se_inimage&VERSION=1.1.1&SERVICE=WMS&REQUEST=GetMap&LLBBOX=137.36632816178096,36.772883864884584,141.8751878470843,40.26195796778519&URL=http%3A%2F%2Flocalhost%3A8180%2Fgeoserver%2Fwms&TILED=true&TILESORIGIN=15291549.700982,4407496.8300991&SRS=EPSG%3A900913&BBOX=15654303.390625,4657155.2587109,15693439.149102,4696291.0171875&WIDTH=256&HEIGHT=256"
/usr/bin/curl $url --connect-timeout 5 --max-time 20 -s -f -o /dev/null
if [ $? -eq 0 ]
then
:
else