前回の続きです。
(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
(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
これでProblem 1は終わりです。次回はProblem 2です。先は長いですね。。。