Read List in Python With X Y Coordinates

Shapely and geometric objects¶

In this lesson, you will learn how to create and manipulate geometries in Python using the Shapely Python Package.

Sources:

These materials are partly based on Shapely-documentation and Westra E. (2013), Chapter 3.

Spatial data model¶

Spatial data model

Fundamental geometric objects that can exist used in Python with Shapely.

The most primal geometric objects are Points , Lines and Polygons which are the basic ingredients when working with spatial data in vector format. Python has a specific module called Shapely for doing various geometric operations. Basic knowledge of using Shapely is fundamental for understanding how geometries are stored and handled in GeoPandas.

Geometric objects consist of coordinate tuples where:

  • Bespeak -object represents a single bespeak in infinite. Points can be either ii-dimensional (x, y) or three dimensional (10, y, z).

  • LineString -object (i.e. a line) represents a sequence of points joined together to course a line. Hence, a line consist of a list of at to the lowest degree 2 coordinate tuples

  • Polygon -object represents a filled area that consists of a list of at least iii coordinate tuples that forms the outerior ring and a (possible) list of hole polygons.

It is too possible to have a collection of geometric objects (e.g. Polygons with multiple parts):

  • MultiPoint -object represents a collection of points and consists of a list of coordinate-tuples

  • MultiLineString -object represents a collection of lines and consists of a listing of line-similar sequences

  • MultiPolygon -object represents a collection of polygons that consists of a list of polygon-like sequences that construct from exterior ring and (possible) hole list tuples

Useful attributes and methods in Shapely include:

  • Creating lines and polygons based on a collection of point objects.

  • Calculating areas/length/premises etc. of input geometries

  • Conducting geometric operations based on the input geometries such as wedlock , difference , distance etc.

  • Conducting spatial queries between geometries such as intersects , touches , crosses , within etc.

Tuple

Tuple is a Python data construction that consists of a number of values separated by commas. Coordinate pairs are often represented as a tuple. For case:

Tuples belong to sequence data types in Python. Other sequence data types are lists and ranges. Tuples have many similarities with lists and ranges, but they are often used for different purposes. The principal difference betwixt tuples and lists is that tuples are immutable, which ways that the contents of a tuple cannot be altered (while lists are mutable; you tin can, for example, add together and remove values from lists).

Point¶

Creating point is piece of cake, you pass x and y coordinates into Indicate() -object (+ perchance likewise z -coordinate):

                            # Import necessary geometric objects from shapely module              from              shapely.geometry              import              Point              ,              LineString              ,              Polygon              # Create Point geometric object(s) with coordinates              point1              =              Indicate              (              2.ii              ,              four.2              )              point2              =              Point              (              7.2              ,              -              25.one              )              point3              =              Betoken              (              9.26              ,              -              2.456              )              point3D              =              Point              (              9.26              ,              -              2.456              ,              0.57              )            

Let's run into what these variables now incorporate:

../../_images/geometric-objects_5_0.svg

Every bit nosotros run across here, Jupyter notebook is able to display the shape straight on the screen.

We tin can apply the impress statement to get information about the actual definition of these objects:

                                print                (                point1                )                print                (                point3D                )              
                POINT (2.two 4.two) Point Z (9.26 -two.456 0.57)              

3D-point can exist recognized from the uppercase Z -alphabetic character in front end of the coordinates.

Let'due south also bank check the data type of a point:

              shapely.geometry.point.Point            

We can meet that the type of the signal is shapely's Betoken. The point object is represented in a specific format based on GEOS C++ library that is one of the standard libraries behind various Geographic Data Systems. Information technology runs nether the hood east.g. in QGIS.

Bespeak attributes and functions¶

Points and other shapely objects have useful built-in attributes and methods. Using the available attributes, we can for example extract the coordinate values of a Point and calculate the Euclidian altitude between points.

geom_type aspect contains information about the geometry type of the Shapely object:

Extracting the coordinates of a Point tin be done in a couple of different ways:

coords aspect contains the coordinate data as a CoordinateSequence which is another data type related to Shapely.

                                # Get xy coordinate tuple                list                (                point1                .                coords                )              

Hither nosotros accept a coordinate tuple inside a list. Using the attributes x and y it is possible to go the coordinates direct equally plain decimal numbers.

                                # Read x and y coordinates separately                ten                =                point1                .                10                y                =                point1                .                y              

It is also possible to summate the altitude between two objects using the distance method. In our case the distance is calculated in a cartesian coordinate system. When working with real GIS data the distance is based on the used coordinate reference system. always check what is the unit of measurement (for case, meters) in the coordinate reference organisation y'all are using.

Let'south calculate the distance betwixt point1 and point2 :

                                    # Check input data                  print                  (                  point1                  )                  print                  (                  point2                  )                
                  POINT (2.two 4.2) POINT (7.two -25.1)                
                                    # Summate the distance between point1 and point2                  dist                  =                  point1                  .                  distance                  (                  point2                  )                  # Print out a nicely formatted info message                  print                  (                  f                  "Distance between the points is                                    {                  dist                  }                                      units"                  )                
                  Altitude between the points is 29.723559679150142 units                

LineString¶

Creating LineString -objects is fairly similar to creating Shapely Points.

Now instead using a single coordinate-tuple nosotros tin can construct the line using either a list of shapely Point -objects or laissez passer the points as coordinate-tuples:

                            # Create a LineString from our Indicate objects              line              =              LineString              ([              point1              ,              point2              ,              point3              ])            
                            # It is as well possible to produce the same outcome using coordinate tuples              line2              =              LineString              ([(              2.2              ,              4.ii              ),              (              7.two              ,              -              25.1              ),              (              9.26              ,              -              2.456              )])            
                            # Check if lines are identical              line              ==              line2            

Let'south see how our line looks like:

../../_images/geometric-objects_28_0.svg

              LINESTRING (2.ii 4.ii, 7.two -25.ane, 9.26 -2.456)            

Equally we can see from to a higher place, the line -variable constitutes of multiple coordinate-pairs.

Check also the data type:

                                # Bank check information blazon of the line object                type                (                line                )              
                shapely.geometry.linestring.LineString              
                            # Check geometry type of the line object              line              .              geom_type            

LineString attributes and functions¶

LineString -object has many useful built-in attributes and functionalities. It is for instance possible to extract the coordinates or the length of a LineString (line), summate the centroid of the line, create points along the line at specific distance, calculate the closest altitude from a line to specified Point and simplify the geometry. See full listing of functionalities from Shapely documentation. Here, nosotros become through a few of them.

We can extract the coordinates of a LineString similarly as with Point

                                    # Become xy coordinate tuples                  list                  (                  line                  .                  coords                  )                
                  [(2.2, 4.two), (7.ii, -25.1), (ix.26, -2.456)]                

Again, we have a list of coordinate tuples (x,y) within a list.

If you would need to admission all x-coordinates or all y-coordinates of the line, you can do it straight using the xy aspect:

                                # Extract ten and y coordinates separately                xcoords                =                listing                (                line                .                xy                [                0                ])                ycoords                =                list                (                line                .                xy                [                1                ])              
                                    print                  (                  xcoords                  )                  print                  (                  ycoords                  )                
                  [two.ii, 7.ii, 9.26] [4.two, -25.ane, -two.456]                

It is possible to retrieve specific attributes such as lenght of the line and center of the line (centroid) directly from the LineString object itself:

                                    # Go the lenght of the line                  l_length                  =                  line                  .                  length                  print                  (                  f                  "Length of our line:                                    {                  l_length                  }                                      units"                  )                
                  Length of our line: 52.46106912939557 units                
                                    # Go the centroid of the line                  print                  (                  line                  .                  centroid                  )                
                  Point (6.229961354035622 -11.89241115757239)                

As y'all tin can run across, the centroid of the line is again a Shapely Bespeak object.

Polygon¶

Creating a Polygon -object continues the same logic of how Point and LineString were created but Polygon object but accepts a sequence of coordinates as input.

Polygon needs at least 3 coordinate-tuples (three points are reguired to form a surface):

                            # Create a Polygon from the coordinates              poly              =              Polygon              ([(              2.2              ,              4.two              ),              (              seven.2              ,              -              25.i              ),              (              9.26              ,              -              2.456              )])            

We can also use information from the Shapely Point objects created earlier, merely we can't use the betoken objects directly. Instead, nosotros need to go data of the x,y coordinate pairs equally a sequence. We can achieve this by using a list comprehension.

                            # Create a Polygon based on information from the Shapely points              poly2              =              Polygon              ([[              p              .              x              ,              p              .              y              ]              for              p              in              [              point1              ,              point2              ,              point3              ]])            

In social club to understand what just happened, let's bank check what the list comprehension produces:

                                [[                p                .                x                ,                p                .                y                ]                for                p                in                [                point1                ,                point2                ,                point3                ]]              
                [[ii.2, 4.two], [seven.ii, -25.1], [ix.26, -two.456]]              

This list of lists was passed every bit input for creating the Polygon.

                            # Check that polygon objects created using two different approaches are identical              poly              ==              poly2            

Let's see how our Polygon looks like

../../_images/geometric-objects_52_0.svg

              POLYGON ((2.ii 4.2, 7.two -25.1, ix.26 -ii.456, two.2 four.2))            

Notice that Polygon representation has double parentheses effectually the coordinates (i.e. POLYGON ((<values in here>)) ). This is considering Polygon tin can besides have holes inside of information technology.

Check also the data type:

              shapely.geometry.polygon.Polygon            
                            # Geometry blazon              poly              .              geom_type            
                            # Check the assist for Polygon objects:              #aid(Polygon)            

Every bit the help of Polygon -object tells, a Polygon can exist synthetic using exterior coordinates and interior coordinates (optional) where the interior coordinates creates a hole within the Polygon:

                            Help              on              Polygon              in              module              shapely              .              geometry              .              polygon              object              :              class              Polygon              (              shapely              .              geometry              .              base of operations              .              BaseGeometry              )              |              A              two              -              dimensional              effigy              divisional              by              a              linear              ring              |              |              A              polygon              has              a              not              -              nil              surface area              .              It              may              take              one              or              more              negative              -              space              |              "holes"              which              are              as well              bounded              by              linear              rings              .              If              any              rings              cross              each              |              other              ,              the              characteristic              is              invalid              and              operations              on              it              may              fail              .              |              |              Attributes              |              ----------              |              exterior              :              LinearRing              |              The              ring              which              premises              the              positive              space              of              the              polygon              .              |              interiors              :              sequence              |              A              sequence              of              rings              which              bound              all              existing              holes              .            

Let's see how we can create a Polygon with a hole:

                            # Ascertain the outer border              border              =              [(              -              180              ,              90              ),              (              -              180              ,              -              90              ),              (              180              ,              -              90              ),              (              180              ,              90              )]            
                                # Outer polygon                globe                =                Polygon                (                trounce                =                border                )                print                (                world                )              
                POLYGON ((-180 xc, -180 -xc, 180 -ninety, 180 ninety, -180 ninety))              

../../_images/geometric-objects_64_0.svg

                            # Permit's create a single big hole where we go out ten units at the boundaries              # Note: at that place could be multiple holes, so we need to provide list of coordinates for the pigsty inside a list              hole              =              [[(              -              170              ,              80              ),              (              -              170              ,              -              80              ),              (              170              ,              -              fourscore              ),              (              170              ,              eighty              )]]            
                                # At present we can construct our Polygon with the hole inside                frame                =                Polygon                (                shell                =                border                ,                holes                =                hole                )                impress                (                frame                )              
                POLYGON ((-180 90, -180 -90, 180 -ninety, 180 ninety, -180 90), (-170 eighty, -170 -80, 170 -eighty, 170 eighty, -170 fourscore))              

Let'southward run into what we accept now:

../../_images/geometric-objects_68_0.svg

As we tin run into the Polygon has now two unlike tuples of coordinates. The first one represents the outerior and the 2d one represents the hole inside of the Polygon.

Polygon attributes and functions¶

Nosotros can over again admission unlike attributes directly from the Polygon object itself that can be really useful for many analyses, such as area , centroid , bounding box , exterior , and exterior-length . See a full list of methods in the Shapely User Manual.

Hither, nosotros tin come across a few of the available attributes and how to access them:

                                    # Print the outputs                  impress                  (                  f                  "Polygon centroid:                                    {                  globe                  .                  centroid                  }                  "                  )                  print                  (                  f                  "Polygon Area:                                    {                  world                  .                  area                  }                  "                  )                  print                  (                  f                  "Polygon Bounding Box:                                    {                  world                  .                  bounds                  }                  "                  )                  print                  (                  f                  "Polygon Outside:                                    {                  globe                  .                  exterior                  }                  "                  )                  print                  (                  f                  "Polygon Exterior Length:                                    {                  world                  .                  exterior                  .                  length                  }                  "                  )                
                  Polygon centroid: POINT (-0 -0) Polygon Area: 64800.0 Polygon Bounding Box: (-180.0, -90.0, 180.0, ninety.0) Polygon Exterior: LINEARRING (-180 90, -180 -xc, 180 -90, 180 90, -180 xc) Polygon Exterior Length: 1080.0                

Every bit we tin can see in a higher place, it is once more fairly straightforward to access different attributes from the Polygon -object. Annotation that distance metrics will make more than sense when we commencement working with information in a projected coordinate system.

Check your understanding¶

Plot these shapes using Shapely!

  • Pentagon, example coords: (30, 2.01), (31.91, 0.62), (31.18, -1.63), (28.82, -one.63), (28.09, 0.62)

  • Triangle

  • Square

  • Cicrle

                                        # Pentagon - Coordinates borrowed from this thread: https://tex.stackexchange.com/questions/179843/make-a-polygon-with-automatically-labelled-nodes-according-to-their-coordinates                    Polygon                    ([(                    30                    ,                    2.01                    ),                    (                    31.91                    ,                    0.62                    ),                    (                    31.18                    ,                    -                    1.63                    ),                    (                    28.82                    ,                    -                    1.63                    ),                    (                    28.09                    ,                    0.62                    )])                  

../../_images/geometric-objects_74_0.svg

                                        # Triangle                    Polygon                    ([(                    0                    ,                    0                    ),                    (                    2                    ,                    4                    ),                    (                    4                    ,                    0                    )])                  

../../_images/geometric-objects_75_0.svg

                                        # Square                    Polygon                    ([(                    0                    ,                    0                    ),                    (                    0                    ,                    4                    ),                    (                    four                    ,                    4                    ),                    (                    4                    ,                    0                    )])                  

../../_images/geometric-objects_76_0.svg

                                        # Circle (using a buffer around a point)                    point                    =                    Point                    ((                    0                    ,                    0                    ))                    point                    .                    buffer                    (                    i                    )                  

../../_images/geometric-objects_77_0.svg

Geometry collections (optional)¶

In some occassions it is useful to store multiple geometries (for instance, several points or several polygons) in a single feature. A applied example would exist a country that is composed of several islands. In such example, all these polygons share the same attributes on the country-level and it might be reasonable to shop that state as geometry collection that contains all the polygons. The attribute table would so contain one row of information with country-level attributes, and the geometry related to those attributes would represent several polygon.

In Shapely, collections of points are implemented past using a MultiPoint -object, collections of curves by using a MultiLineString -object, and collections of surfaces by a MultiPolygon -object.

                            # Import constructors for creating geometry collections              from              shapely.geometry              import              MultiPoint              ,              MultiLineString              ,              MultiPolygon            

Permit's starting time by creating MultiPoint and MultilineString objects:

                                # Create a MultiPoint object of our points 1,2 and 3                multi_point                =                MultiPoint                ([                point1                ,                point2                ,                point3                ])                # It is besides possible to pass coordinate tuples within                multi_point2                =                MultiPoint                ([(                2.two                ,                iv.two                ),                (                7.2                ,                -                25.1                ),                (                9.26                ,                -                two.456                )])                # We tin also create a MultiLineString with ii lines                line1                =                LineString                ([                point1                ,                point2                ])                line2                =                LineString                ([                point2                ,                point3                ])                multi_line                =                MultiLineString                ([                line1                ,                line2                ])                # Print object definitions                impress                (                multi_point                )                print                (                multi_line                )              
                MULTIPOINT (two.2 four.ii, 7.2 -25.one, nine.26 -two.456) MULTILINESTRING ((2.2 4.2, 7.2 -25.ane), (7.two -25.1, nine.26 -ii.456))              

../../_images/geometric-objects_82_0.svg

../../_images/geometric-objects_83_0.svg

MultiPolygons are constructed in a similar manner. Let'due south create a bounding box for "the world" past combinin two split polygons that represent the western and eastern hemispheres.

                                # Let's create the exterior of the western office of the world                west_exterior                =                [(                -                180                ,                ninety                ),                (                -                180                ,                -                ninety                ),                (                0                ,                -                90                ),                (                0                ,                90                )]                # Allow's create a hole --> remember there tin be multiple holes, thus nosotros need to take a listing of hole(due south).                                # Here nosotros accept just ane.                west_hole                =                [[(                -                170                ,                80                ),                (                -                170                ,                -                80                ),                (                -                ten                ,                -                80                ),                (                -                10                ,                80                )]]                # Create the Polygon                west_poly                =                Polygon                (                shell                =                west_exterior                ,                holes                =                west_hole                )                # Print object definition                impress                (                west_poly                )              
                POLYGON ((-180 90, -180 -90, 0 -ninety, 0 90, -180 xc), (-170 80, -170 -lxxx, -ten -80, -x lxxx, -170 80))              

../../_images/geometric-objects_86_0.svg

Shapely also has a tool for creating a bounding box based on minimum and maximum 10 and y coordinates. Instead of using the Polygon constructor, allow's apply the box constructor for creating the polygon:

                            from              shapely.geometry              import              box            
                                # Specify the bbox extent (lower-left corner coordinates and upper-right corner coordinates)                min_x                ,                min_y                =                0                ,                -                xc                max_x                ,                max_y                =                180                ,                xc                # Create the polygon using Shapely                east_poly                =                box                (                minx                =                min_x                ,                miny                =                min_y                ,                maxx                =                max_x                ,                maxy                =                max_y                )                # Print object definition                print                (                east_poly                )              
                POLYGON ((180 -90, 180 ninety, 0 xc, 0 -ninety, 180 -xc))              

../../_images/geometric-objects_90_0.svg

Finally, we can combine the 2 polygons into a MultiPolygon:

                                # Let's create our MultiPolygon. We can pass multiple Polygon -objects into our MultiPolygon as a list                multi_poly                =                MultiPolygon                ([                west_poly                ,                east_poly                ])                # Print object definition                print                (                multi_poly                )              
                MULTIPOLYGON (((-180 90, -180 -90, 0 -90, 0 ninety, -180 ninety), (-170 lxxx, -170 -lxxx, -ten -80, -x 80, -170 80)), ((180 -xc, 180 90, 0 ninety, 0 -xc, 180 -90)))              

../../_images/geometric-objects_93_0.svg

Nosotros can see that the outputs are similar to the basic geometric objects that we created previously but now these objects contain multiple features of those points, lines or polygons.

Convex hull and envelope¶

Convex hull refers to the smalles possible polygon that contains all objects in a drove. Alongside with the minimum bounding box, convex hull is a useful shape when aiming to describe the extent of your data.

Permit's create a convex hull around our multi_point object:

                                    # Bank check input geometry                  multi_point                

../../_images/geometric-objects_95_0.svg

                                    # Convex Hull (smallest polygon around the geometry collection)                  multi_point                  .                  convex_hull                

../../_images/geometric-objects_96_0.svg

                                    # Envelope (smalles rectangular polygon around the geometry collection):                                    multi_point                  .                  envelope                

../../_images/geometric-objects_97_0.svg

Other useful attributes¶

lenght of the geometry collection:

                                    impress                  (                  f                  "Number of objects in our MultiLine:                                    {                  len                  (                  multi_line                  )                  }                  "                  )                  print                  (                  f                  "Number of objects in our MultiPolygon:                                    {                  len                  (                  multi_poly                  )                  }                  "                  )                
                    Number of objects in our MultiLine: 2 Number of objects in our MultiPolygon: ii                  
                    /tmp/ipykernel_46503/3917617097.py:1: ShapelyDeprecationWarning: __len__ for multi-part geometries is deprecated and volition exist removed in Shapely two.0. Cheque the length of the `geoms` property instead to get the  number of parts of a multi-role geometry.   print(f"Number of objects in our MultiLine: {len(multi_line)}") /tmp/ipykernel_46503/3917617097.py:2: ShapelyDeprecationWarning: __len__ for multi-office geometries is deprecated and volition exist removed in Shapely ii.0. Check the length of the `geoms` holding instead to get the  number of parts of a multi-part geometry.   print(f"Number of objects in our MultiPolygon: {len(multi_poly)}")                  

Surface area:

                                    # Print outputs:                  print                  (                  f                  "Expanse of our MultiPolygon:                                    {                  multi_poly                  .                  expanse                  }                  "                  )                  impress                  (                  f                  "Area of our Western Hemisphere polygon:                                    {                  multi_poly                  [                  0                  ]                  .                  area                  }                  "                  )                
                    Area of our MultiPolygon: 39200.0 Area of our Western Hemisphere polygon: 6800.0                  
                    /tmp/ipykernel_46503/498719857.py:iii: ShapelyDeprecationWarning: __getitem__ for multi-office geometries is deprecated and will exist removed in Shapely 2.0. Utilize the `geoms` belongings to admission the elective parts of a multi-part geometry.   print(f"Area of our Western Hemisphere polygon: {multi_poly[0].area}")                  

From the higher up we can see that MultiPolygons accept exactly the aforementioned attributes available as single geometric objects but at present the data such as expanse calculates the area of ALL of the individual -objects combined. We tin likewise access individual objects inside the geometry collections using indices.

Finally, we tin can check if nosotros accept a "valid" MultiPolygon. MultiPolygon is thought as valid if the individual polygons does notintersect with each other. Here, because the polygons take a common 0-meridian, nosotros should Not have a valid polygon. We can check the validity of an object from the is_valid -attribute that tells if the polygons or lines intersect with each other. This can be really useful information when trying to observe topological errors from your information:

                                impress                (                f                "Is polygon valid?:                                {                multi_poly                .                is_valid                }                "                )              

beltranvanctiod.blogspot.com

Source: https://autogis-site.readthedocs.io/en/latest/notebooks/L1/geometric-objects.html

0 Response to "Read List in Python With X Y Coordinates"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel