0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Automating GIS Processes 2024 写経 Exercise 3(Problem 2)

Posted at

前回の続きです。

(2a)

Create a function called get_centroid() that accepts one parameter, geom. The function should take any kind of Shapely’s geometry objects (any instance of shapely.geometry.base.BaseGeometry) as an input, and return the centroid of that geometry.

Make sure to validate the function’s input arguments using assert statements:

  • check that the input is a shapely.geometry.base.BaseGeometry or one of its child classes. Otherwise, raise the error "Input must be a shapely geometry".

こんな感じでしょうか。

BaseGeometry or one of its child classes

の箇所は、isinstance(geom, BaseGeometry)としています。
なお、create_polygon_geometryは前回作成した関数です。

from shapely.geometry.base import BaseGeometry

def get_centroid (geom: BaseGeometry) -> Point:

    assert isinstance(geom, BaseGeometry), "Input must be a shapely geometry"

    return geom.centroid


polygon1 = create_polygon_geometry([(1, 1), (2, 1) ,(2,2)])

centroid = get_centroid(polygon1)
print(centroid)

結果です。比較できるようにpolygon1もprintしています。

image.png

assertionのテストコードとその結果です。

image.png

(2b)

Create a function get_area() accepting one parameter polygon.

The function should accept a shapely.geometry.Polygon and return its area. Again, use assert to make sure the input values are valid, in particular, check that:

  • the input is a shapely.geometry.Polygon. If the argument is anything else, raise an error: "Input should be a shapely.geometry.Polygon".

こんな感じでしょうか。
前述の通り、create_polygon_geometryは前回作成した関数です。

from shapely.geometry import Polygon

def get_area (polygon: Polygon) -> float:

    assert isinstance(polygon, Polygon), "Input should be a shapely.geometry.Polygon"

    return polygon.area


polygon1 = create_polygon_geometry([(1, 1), (2, 1) ,(2,2)])

# NON-EDITABLE CODE CELL FOR TESTING YOUR SOLUTION
area = get_area(polygon1)
print(round(area, 2))

結果です。比較できるようにpolygon1もprintしています。
(面積は1*1/2 = 0.5)
image.png

assertionのテストコードとその結果です。
image.png

(2c)
Create a function get_length() accepting one parameter, geometry.

The function should accept either a shapely.geometry.LineString or a shapely.geometry.Polygon as input. Check the type of the input and return the length of the line if input is a LineString and length of the exterior ring if the input is a Polygon.

If something else is passed to the function, raise an error "‘geometry’ should be either a LineString or a Polygon". Use assert or (advanced, optional) raise a ValueError exception.

こんな感じでしょうか。
今回は、ValueError exceptionraiseしてみました。
get_lengthcreate_polygon_geometryは前回作成した関数です。

from shapely.geometry.base import BaseGeometry
from shapely.geometry import LineString, Polygon

def get_length (geom: BaseGeometry)  -> float:

    if isinstance(geom, LineString) == False and  isinstance(geom, Polygon)  == False: 
        raise ValueError("Input should be a LineStirng or a Polygon") 

    # Check the type of the input and return the length of the line if input is a LineString
    if isinstance(geom, LineString):
        return geom.length
  
    # Check the type of the input and return the length of the exterior ring if the input is a Polygon
    if isinstance(geom, Polygon):
        return geom.exterior.length


line1 = create_line_geometry([Point(1, 1), Point(2, 2)])
line_length = get_length(line1)
print("Line length:", round(line_length,2))

polygon1 = create_polygon_geometry([(1, 1), (2, 1) ,(2,2), (1,2)])
poly_exterior_length = get_length(polygon1)
print("Polygon exterior length:", round(poly_exterior_length,2))

結果です。比較できるようにline1、polygon1もprintしています。
image.png

ValueError exceptionのテストコードとその結果です。
image.png

Docstrings

id you add a docstring to all the functions you defined? If not, add them now :) A short one-line docstring is enough in this exercise.
You can run the code cell below to check all the docstrings:

関数にはdocstring(ドキュメンテーション文字列、ドキュメント文字列)を付けましょう、という話。
詳細はこちら。

次回は Problem 3です。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?