Skip to content

Commit

Permalink
Merge pull request #43 from omanges/transform_changes
Browse files Browse the repository at this point in the history
Added changes for transform_rotate, transform_translate, transform_scale functionality
  • Loading branch information
omanges authored Aug 15, 2020
2 parents 4f76987 + 2bc1eac commit 2652ad9
Show file tree
Hide file tree
Showing 5 changed files with 380 additions and 9 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"Development Status :: 4 - Beta",
"Intended Audience :: Developers",
"Topic :: Software Development :: Libraries",
"License :: OSI Approved :: Apache Software License",
"License :: OSI Approved :: MIT License",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: Python :: 3.6",
Expand Down
50 changes: 50 additions & 0 deletions tests/test_transformation.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
dissolve,
intersect,
union,
transform_rotate,
transform_translate,
transform_scale,
)


Expand Down Expand Up @@ -263,3 +266,50 @@ def test_difference():
[140.0, -26.0],
[140.0, -21.0],
]


def test_transform_rotate():
f = Feature(geometry=Polygon([[[0, 29], [3.5, 29], [2.5, 32], [0, 29]]]))
pivot = [0, 25]
rotated_feature = transform_rotate(f, 10, pivot)

assert rotated_feature["type"] == "Feature"
assert len(rotated_feature["geometry"]["coordinates"][0]) == 4

assert rotated_feature["geometry"]["coordinates"][0] == [
[0.779582, 28.939231],
[4.215029, 28.397872],
[3.837175, 31.512519],
[0.779582, 28.939231],
]


def test_transform_translate():

f = Feature(geometry=Polygon([[[0, 29], [3.5, 29], [2.5, 32], [0, 29]]]))

translate_feature = transform_translate(f, 100, 35, mutate=True)

assert translate_feature["type"] == "Feature"
assert len(translate_feature["geometry"]["coordinates"][0]) == 4
assert translate_feature["geometry"]["coordinates"][0] == [
[0.591903, 29.73668],
[4.091903, 29.73668],
[3.110728, 32.73668],
[0.591903, 29.73668],
]


def test_transform_scale():
f = Feature(geometry=Polygon([[[0, 29], [3.5, 29], [2.5, 32], [0, 29]]]))

translate_feature = transform_scale(f, 3, origin=[0, 29])

assert translate_feature["type"] == "Feature"
assert len(translate_feature["geometry"]["coordinates"][0]) == 4
assert translate_feature["geometry"]["coordinates"][0] == [
[0.0, 29.0],
[10.5, 29.0],
[7.763024, 38.0],
[0.0, 29.0],
]
65 changes: 65 additions & 0 deletions transformation.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,71 @@ f2 = Feature(geometry=Polygon([[
difference(f1, f2)
```

* transform rotate : Rotates any geojson Feature or Geometry of a specified angle, around its centroid or a given pivot point; all rotations follow the right-hand rule

| Argument| Type | Description|
| ------- |------ | ----------- |
| `feature` |Feature | A GeoJSON feature |
| `angle` | float | angle of rotation (along the vertical axis), from North in decimal degrees, negative clockwise |
| `pivot` | list(optional) | point around which the rotation will be performed, deafult values is centroid |
| `mutate` | boolean(optional) | allows GeoJSON input to be mutated (significant performance increase if True), deafult value is False |

| Return | Type | Description |
| ------- | ------ | ----------- |
| `geojson` | Feature | The rotated GeoJSON |

```python
from turfpy.transformation import transform_rotate
from geojson import Polygon, Feature
f = Feature(geometry=Polygon([[[0,29],[3.5,29],[2.5,32],[0,29]]]))
pivot = [0, 25]
transform_rotate(f, 10, pivot)
```

* transform translate : Moves any geojson Feature or Geometry of a specified distance along a Rhumb Line on the provided direction angle.

| Argument| Type | Description|
| ------- |------ | ----------- |
| `feature` |Feature | A GeoJSON feature |
| `distance` | float | length of the motion, negative values determine motion in opposite direction |
| `direction` | float | of the motion, angle from North in decimal degrees, positive clockwise |
| `units` | str(optional) | Unit of distance, default is 'km' refer [Units type](#units-type) section|
| `z_translation` | float(optional) | length of the vertical motion, same unit of distance, default value is 0 |
| `mutate` | boolean(optional) | allows GeoJSON input to be mutated (significant performance increase if True), deafult value is False |

| Return | Type | Description |
| ------- | ------ | ----------- |
| `geojson` | Feature | The translated GeoJSON |

```python
from turfpy.transformation import transform_translate
from geojson import Polygon, Feature
f = Feature(geometry=Polygon([[[0,29],[3.5,29],[2.5,32],[0,29]]]))
transform_translate(f, 100, 35, mutate=True)
```

* transform scale : Scale a GeoJSON from a given point by a factor of scaling (ex: factor=2 would make the GeoJSON 200% larger).
If a FeatureCollection is provided, the origin
point will be calculated based on each individual Feature.

| Argument| Type | Description|
| ------- |------ | ----------- |
| `feature` | Feature | FeatureCollection | A GeoJSON to be scaled |
| `factor` | float | factor of scaling, positive or negative values greater than 0 |
| `origin` | str or list | Point from which the scaling will occur (string options: sw/se/nw/ne/center/centroid), can also provide a point, deafult value is centroid |
| `mutate` | boolean(optional) | allows GeoJSON input to be mutated (significant performance increase if True), deafult value is False |

| Return | Type | Description |
| ------- | ------ | ----------- |
| `geojson` | Feature | The scaled GeoJSON |

```python
from turfpy.transformation import transform_scale
from geojson import Polygon, Feature
f = Feature(geometry=Polygon([[[0,29],[3.5,29],[2.5,32],[0,29]]]))
transform_scale(f, 3, origin=[0, 29])
```

## Units Type
Some functionalities support `units` as a parameter, default values of `units` is `kilometers` for the functionalities that have units are parameters. The values for it are:
```text
Expand Down
13 changes: 7 additions & 6 deletions turfpy/measurement.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
segment_reduce,
)


# ---------- Bearing -----------#


Expand Down Expand Up @@ -438,18 +439,18 @@ def centroid(geojson, properties: dict = None) -> Feature:
"""
x_sum = 0
y_sum = 0
len = 0
length = 0

def _callback_coord_each(
coord, coord_index, feature_index, multi_feature_index, geometry_index
):
nonlocal x_sum, y_sum, len
nonlocal x_sum, y_sum, length
x_sum += coord[0]
y_sum += coord[1]
len += 1
length += 1

coord_each(geojson, _callback_coord_each)
point = Point((x_sum / len, y_sum / len))
point = Point((x_sum / length, y_sum / length))
return Feature(geometry=point, properties=properties if properties else {})


Expand Down Expand Up @@ -1104,7 +1105,7 @@ def calculate_rhumb_bearing(fro, to):
# ------------ rhumb destination -----------#


def rhumb_destination(origin, distance, bearing, options) -> Feature:
def rhumb_destination(origin, distance, bearing, options: dict = {}) -> Feature:
"""
Returns the destination Point having travelled the given distance along a Rhumb line
from the origin Point with the (varant) given bearing.
Expand All @@ -1129,7 +1130,7 @@ def rhumb_destination(origin, distance, bearing, options) -> Feature:
'properties': {"marker-color": "F00"}})
"""
was_negative_distance = distance < 0
distance_in_meters = convert_length(abs(distance), options.get("units", ""), "m")
distance_in_meters = convert_length(abs(distance), options.get("units", "km"), "m")
if was_negative_distance:
distance_in_meters = -1 * (abs(distance_in_meters))
coords = get_coord(origin)
Expand Down
Loading

0 comments on commit 2652ad9

Please sign in to comment.