Geoprocessing using Python - Reading geometries

Python for Spatial Analysis in ArcGIS Using Python in ArcGIS Pro
15 minutes
Share the link to this page


Hi. In this video, we are going to create geometry objects from feature classes, as well as read and write geometries. to control their feature and parts. Please open your London project, which you made in lesson six. Now click on the map, you should have two features layers corresponding to London diversity. One of them is a copy called new feature.

In the previous lessons, we worked a bit with the feature class. What is a feature class? Well, it's a group of common characteristic where all of them have the same spatial representation. For example, London Percy is a class feature with polygons elements. A feature class may hold points, lines or polygons sharing a common set of attributes. Geometry objects hold these elements.

There are seven feature class types. points they represent a small feature that locates any place of interest lines to represent shapes and locations to narrow so they might be used to describe features without an area, such as street center lines, river center lines, pipelines, streams, and so on. polygons to represent shapes and areas like estates, borrows or country Trees among others. There is also a multi point type feature, which is an array of massive points set. So, they are point clusters. Then we have multipatch type, this is a 3d geometry that describes any other surface from a simple to complex object.

And finally, there are annotation type and dimension type, you can access the vertices of a feature using a search cursor. Therefore, a point feature returns a single point object while polyline polygon and multi point features return a set of point objects. is an array of point objects for each feature of the class. Keep in mind that accessing full geometry object is time consuming and the scripts become slow. If you need only a particular geometry property, you can use geometry tokens as shortcuts to reach their properties. For example, you can use the token shape at x y to obtain the tupple of x y coordinate of the features centroid.

Let's try an example using this token. First, right cursor equal arc pi.da dot search and press TAB key Then select a new feature, write comma, open bracket and type in shape at x, y enclose with quotes. Then create a for loop and use the cursor as a list, like in the previous lessons. In this case, row zero contains the tuples of coordinates. So write x comma y equal row, zero, press enter, then print tupple separated by a comma. Now convert the the x and the y in strings and later combine them with a comma in between.

After that, press Enter twice and observe the outcome you will get the center of each feature you can also use the token called shape at through centroid to obtain the centroid. Moreover, if you use the token shape part, you will get the full geometry object. So, in some cases, the center have obtained by shape at shape at x y and shape at true centroid might be slightly different depending on the polygons shape. On the other hand, if you need only the area of the feature, you can use shape area. So, change the previous code and use this token instead of shape at x y when you Run it, you will get the area of each forum. The London diversity feature has also a field called shape underscore area.

So use both the field and the token and then bring both you get the same values. Now, if you have a line feature class to describe the streets geometry within a city, and you need to extract the feature length to calculate the length of several streets, you can use the token shape at length. In previous examples, we got some characteristic of each features. Remember, feature classes such as polygons and polylines. return an array of points object for each feature. If you want to access these object points, you will need an extra for loop, we're going to use the value of the ball field at the beginning of each array.

So we know which are the points of each feature. London diversity class contains 33 features. So for each row, you will get an array of point objects, you will use the get part method to obtain each array. This feature class has only one part. So we'll create a search cursor called cursor using the bow field and shape out token in the new feature class. Then create the first for loop using cursor as list and the object called row to assign each feature, then print the name of the borough using the format method.

After that ness another for loop to the first one. In this case, use the array of object points generated by the second element of a row and the call of the get part method. That is row one dot get part zero, use an object called point to assign each point of the array. In this loop, you will print the value of each x one coordinate using the format method. In this case, you will get the vertices that define each polygon. If you scroll up, you will see the XY coordinates.

But you might not see the level of each borough since there are thousands of points for each one. We will return later with the example where we learn about writing files. Keep in mind that the feature class holds both geometric shape and descriptive attributes. In this lesson, we work with the feature geometry of each borrow. More over the feature class can be made by a single part, or multiple parts. When a feature class in multi part, it doesn't mean that every feature in the feature class is made up of multiple parts.

For example, in Londonderry Per city feature class, each one is made of polygons. But if the feature is multi part, such as an archipelago, you would use multiple polygons to describe that feature. Since each island is a part of the entire archipelago, which is a single, this approach can be applied to multiple points. So in this case, we'll get an array that contains the set up an array of point objects for each part, when you use a search cursor. In a previous lesson, we worked with the describe function and the property shape type to discover if the feature is a point. polyline polygon, multi point or multi part.

But if you want to know if the feature is multi part, you must use the is multi part property of the geometry object that we get from the search cursor. In this case, you will get true if it is made from multiple parts and false otherwise, if we want to know the number of geometry parts of a feature, you can use the part count property of the geometry object. So, let's make a research on the parts of each borough of London diversity feature class in this sense, first create a variable called count. set it to zero Then create a search cursor on the new feature using the borrow field and the shape at token. After that, write a for loop with an object called row and the cursor as list. Then write count, plus equal one, and use that is multi part property of the road to control the if statement.

Then print the name of borrow, which is row zero, and the number of parts using part count property of the row one. Press Enter twice and observe the outcome. You might get surprised with this result. Since there are three multi part features. If you want to print the vertices of each part, you will need three four loops. With the first one, you will go over each feature.

In this case, you get an array of set of points for each part. Then the second one is to go over each set of points. The last one is used to go over each vertex. Finally, when you have polygons with holes keep in mind there are two types of rings, outer and inner ring. A ring is a closed path that defines an area it may be Have any shape. In this case, the outer ring defines the polygon while the inner ring are the holes working with these shapes can be controlled in a similar manner to a multi part feature.

In this case, the outer ring is the first part and the inner rings are the next parts. So rings geometry objects use now points as separators between rings. If you want to separate the vertices of each ring with a statement, you can use the point object in an if statement to control the print. Therefore, if the point value is now print the message in a ring. That's all for this tutorial. I hope you liked it and you will To watch more tutorials about Python and GIS

Sign Up


Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.