Updated answer for Python 3
You should not nowadays encounter any issue while installing on Windows using (as long as pip is version 8 or more according to official Shapely doc and you are using Python 3):
pip install shapely
To check pip version, do :
python -c "import pip;print(pip.__version__)"
If you need to upgrade pip, just do :
pip install -U pip
Outdated if Python 3 otherwise, always relevant for Python 2.x
You're doing your installation wrong.
Instead of
pip install shapely
go to https://www.lfd.uci.edu/~gohlke/pythonlibs/#shapely to download Windows wheels
Click on the file Shapely‑1.6.4.post2‑cp27‑cp27m‑win_amd64.whl
Shapely‑1.6.4 is the version of Shapely, cp27‑cp27m means the wheel is for Python 2.7, win_amd64 means it's for Windows with 64 bits architecture (you may choose another wheel depending on your Python version and Windows architecture)
Launch the install with the following and it will be OK after.
pip install Shapely‑1.6.4.post2‑cp27‑cp27m‑win_amd64.whl
Just as an information, "pip install shapely" works when you have the C compiler installed to build Geos dll and it's not the way it works on Windows.
Use rasterio of Sean Gillies. It can be easily combined with Fiona (read and write shapefiles) and shapely of the same author.
In the script rasterio_polygonize.py
the beginning is
import rasterio
from rasterio.features import shapes
mask = None
with rasterio.Env():
with rasterio.open('a_raster') as src:
image = src.read(1) # first band
results = (
{'properties': {'raster_val': v}, 'geometry': s}
for i, (s, v)
in enumerate(
shapes(image, mask=mask, transform=src.transform)))
The result is a generator of GeoJSON features
geoms = list(results)
# first feature
print geoms[0]
{'geometry': {'type': 'Polygon', 'coordinates': [[(202086.577, 90534.3504440678), (202086.577, 90498.96207), (202121.96537406777, 90498.96207), (202121.96537406777, 90534.3504440678), (202086.577, 90534.3504440678)]]}, 'properties': {'raster_val': 170.52000427246094}}
That you can transform into shapely geometries
from shapely.geometry import shape
print shape(geoms[0]['geometry'])
POLYGON ((202086.577 90534.35044406779, 202086.577 90498.96206999999, 202121.9653740678 90498.96206999999, 202121.9653740678 90534.35044406779, 202086.577 90534.35044406779))
Create geopandas Dataframe and enable easy to use functionalities of spatial join, plotting, save as geojson, ESRI shapefile etc.
geoms = list(results)
import geopandas as gp
gpd_polygonized_raster = gp.GeoDataFrame.from_features(geoms)
Best Answer
If you want to mix GDAL/OGR and Shapely you need specifically use 'loads' method from shapely.wkt python module. For instance, next code does the job:
After running it, it was printed at Python Console each representative_point in WKT format.