Skip to content

Commit

Permalink
Change how centroids are computed on MultiPolygons
Browse files Browse the repository at this point in the history
  • Loading branch information
mthh committed Jan 9, 2017
1 parent ef9085f commit 9261ebb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
11 changes: 7 additions & 4 deletions README.rst
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ smoomapy
Make smoothed maps in your python environnement
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

|Build Status| |Version| |Coveralls|
|Build Status Travis| |Build Status Appveyor| |Version| |Coveralls|

More or less a python port of *Stewart method* from R SpatialPositon
package (https://github.com/Groupe-ElementR/SpatialPosition/).
package (https://github.com/Groupe-ElementR/SpatialPosition/).

Allow to set a desired number of class and choose discretization method or
directly set some custom breaks values.
directly set some custom breaks values.

Input/output can be a path to a geographic layer (GeoJSON, shp, etc.) or a GeoDataFrame.

Expand Down Expand Up @@ -111,9 +111,12 @@ From github :
$ cd smoomapy/
$ python setup.py install
.. |Build Status| image:: https://travis-ci.org/mthh/smoomapy.svg?branch=master
.. |Build Status Travis| image:: https://travis-ci.org/mthh/smoomapy.svg?branch=master
:target: https://travis-ci.org/mthh/smoomapy

.. |Build Status Appveyor| image:: https://ci.appveyor.com/api/projects/status/tc7ynr2ejpp8a4c9/branch/master?svg=true
:target: https://ci.appveyor.com/project/mthh/smoomapy/branch/master

.. |Version| image:: https://img.shields.io/pypi/v/smoomapy.svg
:target: https://pypi.python.org/pypi/smoomapy

Expand Down
22 changes: 19 additions & 3 deletions smoomapy/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,17 @@ def make_regular_points(bounds, resolution):
(len(prog_x), len(prog_y)))


def _compute_centroids(geometries):
res = []
for geom in geometries:
if geom.is_simple:
res.append(geom.centroid)
else:
ix_biggest = np.argmax([g.area for g in gg])
res.append(geom[ix_biggest].centroid)
return res


def make_dist_mat(xy1, xy2, longlat=False):
"""
Return a distance matrix between two set of coordinates.
Expand Down Expand Up @@ -427,10 +438,16 @@ def compute_pot(self, variable_name, span, beta,
self.unknownpts, self.shape = make_regular_points(bounds, resolution) \
if resolution else make_regular_points_with_no_res(bounds)

if all(i in ("Polygon", "Point") for i in knownpts.geom_type.values):
centroids = knownpts.geometry.centroid if not self.longlat else knownpts.to_crs({"init": "epsg:4326"}).geometry.centroid

else:
centroids = _compute_centroids(knownpts.geometry if not self.longlat else knownpts.to_crs({"init": "epsg:4326"}).geometry)

if not self.longlat:
knwpts_coords = np.array([
(g.coords.xy[0][0], g.coords.xy[1][0])
for g in knownpts.geometry.centroid])
for g in centroids])

mat_dist = make_dist_mat(knwpts_coords,
self.unknownpts,
Expand All @@ -439,8 +456,7 @@ def compute_pot(self, variable_name, span, beta,
else:
knwpts_coords = np.array([
[g.coords.xy[1][0], g.coords.xy[0][0]]
for g in knownpts.to_crs(
{"init": "epsg:4326"}).geometry.centroid])
for g in centroids])

natearth = pyproj.Proj("+proj=natearth")
wgs84 = pyproj.Proj("+init=EPSG:4326")
Expand Down

0 comments on commit 9261ebb

Please sign in to comment.