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 1 commit
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
Next Next commit
Added changes to add new section random and adde two new functionalit…
…y i.e random position and random points
  • Loading branch information
omanges committed Dec 20, 2020
commit bb5e7b6e9f98cdb3480a49fde8abd535c797d05f
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
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)