Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How move a multipolygon with geopandas in python2

I'm novice in GIS world in python (geopandas, shapely, etc). I need "move" a Multipolygon upwards, but I don't know how to do that.

The Problem

import pandas as pd
import numpy as np
import matplotlib
from matplotlib import pyplot as plt
import seaborn as sns
import pysal as ps
from pysal.contrib.viz import mapping as maps    
import geopandas as gpd

fs = 5
nums = ("renta", 1)

f, ax = plt.subplots(1, figsize=(fs,fs))

spain.plot(column="XBAR", ax=ax, linewidth=0.05, cmap="OrRd", scheme="unique_values")
ax.set_axis_off()

plt.title("RENTA MEDIA", fontsize=20)
plt.tight_layout()

plt.savefig("../imgs/map_%s_%s.svg" % nums, bbox_iches="tight", dpi=2800)
plt.show()

output:

As you can see, Islands "Canarias" are away from rest of Spain, I want a smaller figure, and it's takes account that color is important, it's represent income mean for each counties.

If it's helps:

canarias = spain[spain.ca == "CANARIAS"]["geometry"]
print canarias
print canarias.type

13    (POLYGON ((-17.92791748046875 27.8495826721191...
Name: geometry, dtype: object
13    MultiPolygon
dtype: object

Note: Are counties missing, that's why I don't have data for this counties.

My first attempt was try to change coords of polygon for example, I try to find how to do some like this: canarias.coords + (-3,-4) but I didn't find how to access to coords of multipolygon to do that.

I appreciate any help.

PS: Sorry for my English :-/

like image 561
mmngreco Avatar asked Sep 15 '16 16:09

mmngreco


People also ask

How to dissolve polygons in Python?

Learn how to dissolve polygons in Python using GeoPandas. When you dissolve polygons, you remove the interior boundaries of a set of polygons with the same attribute value and create one new merged or combined polygon for each attribute value. Learn how to dissolve polygons in Python using GeoPandas. Tutorials Courses Workshops Tools Blog About

How to convert multipolygon to single polygon?

I would like to convert them to Polygons i.e. fill in the holes of multipolygon and make it a single polygon. from shapely.geometry import MultiPolygon, Polygon gdf ['Polygon'] = gdf ['SHAPE'].apply ( lambda x: MultiPolygon (Polygon (p.exterior) for p in x))

Is there a way to explode polygons in geodataframe?

It depends on how do you want to treat the data attached to those polygons. If you don't care about them and are interested only in geometries, one option is to dissolve the whole GeoDataFrame and then explode it.

Can geopandas create vectors grid from a shapefile?

The input geometry is MultiPolygon and I've previously created with SQL the output table: I'm be able to create with GeoPandas a vectors grid from a shapefile and save it as another shapefile.


1 Answers

I think you are looking for shapely.affinity.translate. From the docs:

Signature: shapely.affinity.translate(geom, xoff=0.0, yoff=0.0, zoff=0.0)
Docstring:
Returns a translated geometry shifted by offsets along each dimension.

The general 3D affine transformation matrix for translation is:

/ 1  0  0 xoff \ 
| 0  1  0 yoff |
| 0  0  1 zoff |
\ 0  0  0   1  /

For your specific example, you can use:

canarias = spain[spain.ca == "CANARIAS"].geometry
canarias_shift = canarias.apply(lambda x: shapely.affinity.translate(x, xoff=-3, yoff=-4))
like image 119
jdmcbr Avatar answered Oct 02 '22 02:10

jdmcbr