Skip to content

Instantly share code, notes, and snippets.

@Kelvinrr
Last active August 24, 2020 18:17
Show Gist options
  • Select an option

  • Save Kelvinrr/d42a3dad1877eeaeb076c38abe6759df to your computer and use it in GitHub Desktop.

Select an option

Save Kelvinrr/d42a3dad1877eeaeb076c38abe6759df to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import pandas as pd \n",
"import datashader.transfer_functions as tf\n",
"import datashader as ds\n",
"from collections import OrderedDict as odict\n",
"from sqlalchemy import create_engine\n",
"from shapely import wkt\n",
"\n",
"import gdal"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# /scratch/krodriguez/kaguyageoms.csv was generated with the query:\n",
"# \\copy (Select st_x(geom) as x, st_y(geom) as y From kaguyasp.points) To '/scratch/krodriguez/kaguyageoms.csv' With CSV header\n",
"\n",
"df = pd.read_csv(\"/scratch/krodriguez/kaguyageoms.csv\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>x</th>\n",
" <th>y</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>185.339672</td>\n",
" <td>21.872527</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>189.963815</td>\n",
" <td>-1.120378</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>185.331087</td>\n",
" <td>21.502996</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>189.954315</td>\n",
" <td>-1.568647</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>176.669999</td>\n",
" <td>-79.155373</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>73832917</th>\n",
" <td>161.296746</td>\n",
" <td>40.379175</td>\n",
" </tr>\n",
" <tr>\n",
" <th>73832918</th>\n",
" <td>161.294383</td>\n",
" <td>40.488716</td>\n",
" </tr>\n",
" <tr>\n",
" <th>73832919</th>\n",
" <td>163.713332</td>\n",
" <td>28.022364</td>\n",
" </tr>\n",
" <tr>\n",
" <th>73832920</th>\n",
" <td>163.711403</td>\n",
" <td>28.131928</td>\n",
" </tr>\n",
" <tr>\n",
" <th>73832921</th>\n",
" <td>162.899690</td>\n",
" <td>8.838021</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>73832922 rows × 2 columns</p>\n",
"</div>"
],
"text/plain": [
" x y\n",
"0 185.339672 21.872527\n",
"1 189.963815 -1.120378\n",
"2 185.331087 21.502996\n",
"3 189.954315 -1.568647\n",
"4 176.669999 -79.155373\n",
"... ... ...\n",
"73832917 161.296746 40.379175\n",
"73832918 161.294383 40.488716\n",
"73832919 163.713332 28.022364\n",
"73832920 163.711403 28.131928\n",
"73832921 162.899690 8.838021\n",
"\n",
"[73832922 rows x 2 columns]"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"# change the size here to control granularity,\n",
"# we aren't so much making a heatmap so much as a 2-D histogram, but works well for our purposes.\n",
"# Probably look much better when all points a loaded. \n",
"\n",
"hist = ds.Canvas(plot_width=400, plot_height=200).points(df,'x','y')\n",
"\n",
"# colorblind safe cmap from: https://colorbrewer2.org/#type=diverging&scheme=RdBu&n=11\n",
"colors = ['#67001f','#b2182b','#d6604d','#f4a582','#fddbc7','#d1e5f0','#92c5de','#4393c3','#2166ac','#053061'][::-1]\n",
"hist = tf.shade(hist, cmap=colors, how='linear', alpha=255, min_alpha=40)\n",
"\n",
"# comment out this line to print image in jupyter, just careful if you are making a very large image\n",
"hist"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Save to disk\n",
"from PIL import ImageFilter\n",
"\n",
"itiff = '/home/krodriguez/heatmap.tif'\n",
"img = hist.to_pil()\n",
"\n",
"# img = img.filter(ImageFilter.GaussianBlur(1))\n",
"img.save(open(itiff, 'bw'))"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"temp = '/tmp/tmp.tif'\n",
"otiff = '/home/krodriguez/heatmap_spatial.tif'\n",
"\n",
"# Add spatial data to the image\n",
"# sometimes this is flakey and you have to run the command's equivilent opn the command line \n",
"_ = gdal.Translate(otiff, itiff, outputBounds= (0, 90, 360, -90), outputSRS='EPSG:4326')"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "AutocnetDev",
"language": "python",
"name": "autocnetdev"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment