Automating GIS Processes 2024 写経　Exercise １（Problem 1残り）

[前回](https://qiita.com/t_t_japan/items/2807123d91f1015f7672)の続きです。

https://github.com/Automating-GIS-processes-II-2024/exercise-1/blob/main/Exercise-1-problem-1-2.ipynb

## (1b)
> Create a function called create_line_geometry() that takes a list of shapely.geometry.Points as an argument, and returns a shapely.geometry.LineString object of those input points.

> In addition, you should validate the function input using assert statements (see lesson 6 of the Geo-Python course and the hints for this exercise):
> - Inside the function, first check that the input is a list. If something else than a list is passed, raise the following error: "Input should be a list".
> - Use assert to check that the input list contains at least two values. Otherwise, raise the error: "At minimum two points are required for a LineString"
> - (optional) Use assert to check that all values in the input list are shapely.geometry.Points. Otherwise, raise the error: "All list values must be of type shapely.geometry.Point"


こんな感じでしょうか。今回からは、できるだけ型の定義も入れるようにします。

```
from shapely.geometry import Point, LineString 

def create_line_geometry (points: list[Point]) -> LineString:

    # Inside the function, first check that the input is a list. If something else than a list is passed, raise the following error: "Input should be a list".
    assert isinstance(points, list), "Input should be a list"

    #Use assert to check that the input list contains at least two values. Otherwise, raise the error: "At minimum two points are required for a LineString"
    assert len(points) >1,  "At minimum two points are required for a LineString"

    # (optional) Use assert to check that all values in the input list are shapely.geometry.Points. Otherwise, raise the error: "All list values must be of type shapely.geometry.Point"
    for point in points:
         assert isinstance(point, Point), "All list values must be of type shapely.geometry.Point"

    return LineString(points)
```

テストコードです。
```
# NON-EDITABLE CODE CELL FOR TESTING YOUR SOLUTION
print(line1)
print(line1.geom_type)


# NON-EDITABLE CODE CELL FOR TESTING YOUR SOLUTION
try:
    # Pass something else than a list
    create_line_geometry("Give me a line!")
except AssertionError as exception:
    print(f"The function (correctly) detected an error. The error message was ‘{exception.args[0]}’")
except Exception as exception:
    raise exception


try:
    # Pass single point
    create_line_geometry([Point(45.2, 22.34)])
except AssertionError as exception:
    print(f"The function (correctly) detected an error. The error message was ‘{exception.args[0]}’")
except Exception as exception:
    raise exception


try:
    # Pass non-point
    create_line_geometry([Point(45.2, 22.34), "aaaa"])
except AssertionError as exception:
    print(f"The function (correctly) detected an error. The error message was ‘{exception.args[0]}’")
except Exception as exception:
    raise exception

```
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/4076503/eb59e6e8-b7af-4e3b-b2e9-eb9317cc19d2.png)

## (1c)

> Create a function create_polygon_geometry() that accepts one parameter coordinates. coordinates should be a list of coordinate tuples. The function should create and return a shapely.geometry.Polygon object based on these coordinates.

> Again, use assert statements to ensure the input arguments are valid:
> - first check that the input is a list. If something else than a list is passed, raise the following error: "Input should be a list".
> - Check that the input list contains at least three values. Otherwise, raise the error: "At minimum three points are required for a polygon"
> - Check that all values in the input list are tuples of two values. Otherwise, raise the error: "All list values must be coordinate tuples"
> - (optional) check that all tuples’ values are instances of either int or float.


こんな感じでしょうか。
```
from shapely.geometry import Point, Polygon  

def create_polygon_geometry (coordinates: list[Point]) -> Polygon:

    # first check that the input is a list. If something else than a list is passed, raise the following error: "Input should be a list".
    assert isinstance(coordinates, list), "Input should be a list"

    # Check that the input list contains at least three values. Otherwise, raise the error: "At minimum three points are required for a polygon"
    assert len(coordinates) > 2,  "At minimum three points are required for a polygon"

    # Check that all values in the input list are tuples of two values. Otherwise, raise the error: "All list values must be coordinate tuples"
    for coordinate in coordinates:
         assert isinstance(coordinate, tuple), "All list values must be coordinate tuples"
         assert len(coordinate) == 2, "All list values must be coordinate tuples"

    # (optional) check that all tuples’ values are instances of either int or float.
    for coordinate in coordinates:
        for value in coordinate:
            assert isinstance(value, int) or isinstance(value, float), "All list values must be int or float"

    return Polygon(coordinates)
```
テストコードです。
```
polygon1 = create_polygon_geometry([(45.2, 22.34), (100.22, -3.20), (70.0, 10.20)])

# NON-EDITABLE CODE CELL FOR TESTING YOUR SOLUTION
print(polygon1)
print(polygon1.geom_type)

try:
    # Pass something else than a list
    create_polygon_geometry("Give me a polygon")
except AssertionError as exception:
    print(f"The function (correctly) detected an error. The error message was ‘{exception.args[0]}’")
except Exception as exception:
    raise exception

try:
    # Pass 2 points
    create_polygon_geometry([(45.2, 22.34), (100.22, -3.20)])
except AssertionError as exception:
    print(f"The function (correctly) detected an error. The error message was ‘{exception.args[0]}’")
except Exception as exception:
    raise exception

try:
    # Pass list of lists
    create_polygon_geometry([[45.2, 22.34], [100.22, -3.20],[70.0, 10.20]])
except AssertionError as exception:
    print(f"The function (correctly) detected an error. The error message was ‘{exception.args[0]}’")
except Exception as exception:
    raise exception

try:
    # Pass 3-value points
    create_polygon_geometry([(45.2, 22.34, 100), (100.22, -3.20, 200), (70.0, 10.20, 300)])
except AssertionError as exception:
    print(f"The function (correctly) detected an error. The error message was ‘{exception.args[0]}’")
except Exception as exception:
    raise exception

try:
    # Pass tuple of Strings
    create_polygon_geometry([("a","b"), ("a","b"), ("a","b")])
except AssertionError as exception:
    print(f"The function (correctly) detected an error. The error message was ‘{exception.args[0]}’")
except Exception as exception:
    raise exception
```

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/4076503/761f6c1a-64f1-4644-88d7-e9933e638312.png)



これでProblem 1は終わりです。次回はProblem 2です。先は長いですね。。。