Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added changes for Random position and Random Points #81

Merged
merged 3 commits into from
Jan 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ It supports below features:

- [Misc](https://github.com/omanges/turfpy/blob/master/misc.md)

- [Random](https://github.com/omanges/turfpy/blob/master/random.md)

## Documentation

Documentation can be found at: [docs](https://turfpy.readthedocs.io/en/latest/)
Expand Down
7 changes: 7 additions & 0 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ A Python library for performing geospatial data analysis which reimplements `tur
Line Intersect <misc/line_intersect>
Line Segment <misc/line_segment>

.. toctree::
:maxdepth: 1
:caption: Random

Random Position <random/random_position>
Random Points <random/random_points>


.. toctree::
:maxdepth: 1
Expand Down
44 changes: 44 additions & 0 deletions docs/source/random/random_points.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
Random Points
==============

Generates geojson random points, if bbox provided then the generated points will be in the bbox.

Example
-------

.. jupyter-execute::

from turfpy.random import random_points

random_points(count=3, bbox=[11.953125, 18.979025953255267, 52.03125, 46.558860303117164])


Interactive Example
-------------------

.. jupyter-execute::

from geojson import Feature
from ipyleaflet import Map, GeoJSON
from turfpy.random import random_points
from turfpy.measurement import bbox_polygon

bb = [11.953125, 18.979025953255267, 52.03125, 46.558860303117164]

fc = random_points(count=3, bbox=[11.953125, 18.979025953255267, 52.03125, 46.558860303117164])

geo_json = GeoJSON(name="Points", data=fc)



bbox_polygon_geojson = GeoJSON(
name="Bounding Box Polygon", data=bbox_polygon(bb), style={"color": "red"}
)


m = Map(center=[4.889835742990713, 5.82601547241211], zoom=1)

m.add_layer(geo_json)
m.add_layer(bbox_polygon_geojson)

m
51 changes: 51 additions & 0 deletions docs/source/random/random_position.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
Random Position
================

Generates a random position, if bbox provided then the generated position will be in the bbox.

Example
-------

.. jupyter-execute::

from turfpy.random import random_position

random_position(bbox=[11.953125, 18.979025953255267, 52.03125, 46.558860303117164])


Interactive Example
-------------------

.. jupyter-execute::

from geojson import Feature
from ipyleaflet import Map, GeoJSON
from turfpy.random import random_position
from turfpy.measurement import bbox_polygon

bb = [11.953125, 18.979025953255267, 52.03125, 46.558860303117164]

random_pos = random_position(bbox=bb)

f = Feature(
geometry={
"coordinates": random_pos,
"type": "Point",
}
)

geo_json = GeoJSON(name="Position", data=f)



bbox_polygon_geojson = GeoJSON(
name="Bounding Box Polygon", data=bbox_polygon(bb), style={"color": "red"}
)


m = Map(center=[4.889835742990713, 5.82601547241211], zoom=1)

m.add_layer(geo_json)
m.add_layer(bbox_polygon_geojson)

m
71 changes: 71 additions & 0 deletions examples/Random.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Random\n",
"This notebook demonstrates random functionality examples"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Random position\n",
"Generates a random position, if bbox provided then the generated position will be in the bbox."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from turfpy.random import random_position\n",
"\n",
"random_position(bbox=[11.953125, 18.979025953255267, 52.03125, 46.558860303117164])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Random points\n",
"Generates geojson random points, if bbox provided then the generated points will be in the bbox"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from turfpy.random import random_points\n",
"\n",
"random_points(count=3, bbox=[11.953125, 18.979025953255267, 52.03125, 46.558860303117164])"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.9"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
2 changes: 1 addition & 1 deletion examples/measurements.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.3"
"version": "3.7.9"
}
},
"nbformat": 4,
Expand Down
33 changes: 33 additions & 0 deletions random.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
## Random Examples :
* Random Position : Generates a random position, if bbox provided then the generated position will be in the bbox.

| Argument | Type | Description |
| ------- | ------ | ----------- |
| `bbox` | list | Bounding Box in which position to be generated |

| Return | Type | Description |
| ------- | ------ | ----------- |
| `position` | list | A position as coordinates|

```python
from turfpy.random import random_position

random_position(bbox=[11.953125, 18.979025953255267, 52.03125, 46.558860303117164])
```

* Random Points : Generates geojson random points, if bbox provided then the generated points will be in the bbox.

| Argument | Type | Description |
| ------- | ------ | ----------- |
| `count` | int | Number of points to be generated, default value is one |
| `bbox` | list | Bounding Box in which points are to be generated |

| Return | Type | Description |
| ------- | ------ | ----------- |
| `points` | FeatureCollection | A FeatureCollection of generated points |

```python
from turfpy.random import random_points

random_points(count=3, bbox=[11.953125, 18.979025953255267, 52.03125, 46.558860303117164])
```
55 changes: 55 additions & 0 deletions tests/test_random.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"""
Test module for randoms.
"""
from geojson import Feature, Point

from turfpy.measurement import bbox, boolean_point_in_polygon
from turfpy.random import random_points, random_position


def test_random_position():
data = {
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[11.953125, 18.979025953255267],
[52.03125, 18.979025953255267],
[52.03125, 46.558860303117164],
[11.953125, 46.558860303117164],
[11.953125, 18.979025953255267],
]
],
},
}

pos = random_position(bbox=bbox(data))
assert len(pos) == 2
pos = Feature(geometry=Point(pos))
assert boolean_point_in_polygon(point=pos, polygon=data)


def test_random_points():
data = {
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[11.953125, 18.979025953255267],
[52.03125, 18.979025953255267],
[52.03125, 46.558860303117164],
[11.953125, 46.558860303117164],
[11.953125, 18.979025953255267],
]
],
},
}

pos = random_points(count=3, bbox=bbox(data))
assert len(pos["features"]) == 3
for point in pos["features"]:
assert boolean_point_in_polygon(point=point, polygon=data)
73 changes: 73 additions & 0 deletions turfpy/random.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
"""
This module implements some of the methods that allows to
generate some random spatial data.
This is mainly inspired by turf.js.
link: http://turfjs.org/
"""
import random

from geojson import Feature, FeatureCollection, Point


def random_position(bbox: list = None):
"""
Generates a random position, if bbox provided then the
generated position will be in the bbox.

:param bbox: Bounding box extent in west, south, east, north order
:return: A position as coordinates.

Exmample:

>>> from turfpy.random import random_position
>>> random_position(bbox=[11.953125,
>>> 18.979025953255267, 52.03125, 46.558860303117164])
"""
if not bbox:
return [lon(), lat()]

if len(bbox) != 4:
raise Exception("bbox with 4 positions are only supported")

return coord_in_bbox(bbox)


def rnd():
return random.random() - 0.5


def lon():
return rnd() * 360


def lat():
return rnd() * 180


def coord_in_bbox(bbox: list):
return [
random.random() * (bbox[2] - bbox[0]) + bbox[0],
random.random() * (bbox[3] - bbox[1]) + bbox[1],
]


def random_points(count: int = 1, bbox: list = None) -> FeatureCollection:
"""
Generates geojson random points, if bbox provided then the
generated points will be in the bbox.

:param count: Number of points to be generated, default value is one.
:param bbox: Bounding box extent in west, south, east, north order
:return: A FeatureCollection of generated points.

Exmample:

>>> from turfpy.random import random_points
>>> random_points(count=3, bbox=[11.953125,
>>> 18.979025953255267, 52.03125, 46.558860303117164])
"""
features = []
for i in range(count):
features.append(Feature(geometry=Point(random_position(bbox))))

return FeatureCollection(features)