Skip to content

Instantly share code, notes, and snippets.

@mthh
Last active November 4, 2015 14:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mthh/3f1bfd28d5b3322ebcf8 to your computer and use it in GitHub Desktop.
Save mthh/3f1bfd28d5b3322ebcf8 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
"""
@author: mthh
"""
import geopandas as gpd
import numpy as np
import matplotlib.pyplot as plt
import gpd_lite_toolbox as glt
path_points = 'pts_umz_2700s.shp'
path_polygons = 'eu32_poids.shp'
def timed_return(my_func, printed=False):
import time
"""
Add a time (in sec.) to the return result(s) of the decorated function
"""
def timed(*args, **kw):
""" The decorator """
start_time = time.time()
result = my_func(*args, **kw)
if printed:
print('{}: {:.3f}s'.format(my_func.__name__,
time.time()-start_time))
return result
else:
return result, round(time.time()-start_time, 3)
return timed
#######################
### Test gridify_data :
gdf = gpd.read_file(path_points).to_crs(epsg=3035)
@timed_return
def a():
return glt.gridify_data(gdf, 8000, 'time', method=np.min)
res, elapsed_time = a()
res.plot(column='time')
print('Gridify_data : {:.2f}s (on {} source points)'.format(elapsed_time, len(gdf)))
#######################
### Test mean_coordinates :
@timed_return
def a():
return glt.mean_coordinates(gdf, id_field='id_route', weight_field='time')
res, elapsed_time = a()
res.plot()
print('\nmean_coordinates : {:.2f}s (on {} source points)'.format(elapsed_time, len(gdf)))
#######################
#######################
### Test transform_cartogram :
gdf = gpd.read_file(path_polygons)
gdf.plot()
@timed_return
def a():
return glt.transform_cartogram(gdf, 'SIZE(MB)', 4, inplace=False)
res, elapsed_time = a()
res.plot()
print('\ntransform_cartogram : {:.2f}s'.format(elapsed_time))
#######################
### Test non_contiguous_cartogram :
@timed_return
def a():
return glt.non_contiguous_cartogram(gdf, 'SIZE(MB)', 10, buff_kws={'resolution': 3})
res, elapsed_time = a()
res.plot()
print('\nnon_contiguous_cartogram : {:.2f}s'.format(elapsed_time))
#######################
### Test random_point_on_surface :
@timed_return
def a():
return glt.random_pts_on_surface(gdf, coef=12)
res, elapsed_time = a()
res.plot()
print('\nmean_coordinates : {:.2f}s (on {} generated points)'.format(elapsed_time, len(res)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment