Skip to main content

This tools takes geometries to describe areas to in- and exclude, and generates a list of points that form a grid covering the described area.

Project description

Gridify takes geometries in a geopandas dataframe to describe areas to in- and exclude. It then generates a grid of box shaped polygons covering the include area.

How to use

Import packages

import geopandas as gpd
import shapely.geometry
import matplotlib.pyplot as plt

from gridify.gridify import gridify

Define an include area consisting of a polygon and a line part. And put it in a geodataframe.

part1 = shapely.geometry.box(
   minx=0,
   miny=0,
   maxx=0.5,
   maxy=1,
)
part2 = shapely.geometry.LineString(
   [
       (0.5, 0),
       (5 / 6, 1.0),
   ]
)
include_gdf = gpd.GeoDataFrame({"col1": [1, 2]}, geometry=[part1, part2])
ax = include_gdf.plot(column="col1")
ax.set_xlim([-.1, 1.2])
ax.set_ylim([-.1, 1.2])
docs/_static/figs/output_3_1.png

Define an area to exclude

exclude = shapely.geometry.box(
    minx=0.5,
    miny=0.5,
    maxx=1.1,
    maxy=1.1,
)
exclude_gdf = gpd.GeoDataFrame({"col1": [1]}, geometry=[exclude])
ax = exclude_gdf.plot(color="red", alpha=0.5)
ax.set_xlim([-.1, 1.2])
ax.set_ylim([-.1, 1.2])
docs/_static/figs/output_5_1.png

Use include area and exclude area to define a grid with (1/3) as grid size. Include partial overlap with the exclusion area into the grid.

grid = gridify(
    include_area=include_gdf,
    exclude_area=exclude_gdf,
    grid_size=((1/3), (1/3)),
    include_partial=False,
)

ax = grid.boundary.plot()
ax.set_xlim([-.1, 1.2])
ax.set_ylim([-.1, 1.2])
docs/_static/figs/output_7_1.png

Plot the grid overlapping the include area in green, and the exclude area in red.

ax = include_gdf.plot(color="green", alpha=0.5)
exclude_gdf.plot(ax=ax, color="red", alpha=0.5)
grid.boundary.plot(ax=ax, color="blue")
docs/_static/figs/output_9_1.png

Alternatively, partial overlap may be included into the final grid.

grid_include_partial = gridify(
    include_area=include_gdf,
    exclude_area=exclude_gdf,
    grid_size=((1/3), (1/3)),
    include_partial=True,
)

ax = include_gdf.plot(color="green", alpha=0.5)
exclude_gdf.plot(ax=ax, color="red", alpha=0.5)
grid_include_partial.boundary.plot(ax=ax, color="blue")
docs/_static/figs/output_11_1.png

Installation

To install gridify, do:

git clone https://gitlab.com/rwsdatalab/codebase/image/gridify.git
cd gridify
pip install .

Run tests (including coverage) with:

pip install -r requirements-dev.txt
python setup.py test

Documentation

Include a link to your project’s full documentation here.

License

Copyright 2022 Rijkswaterstaat

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gridify-0.1.2.tar.gz (8.8 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page