Pythongdal Tutorial: filters, simple spatial analysis, functions, and modules

Source: Internet
Author: User
Pythongdal Tutorial: filter, simple space analysis, function and module Layer object has a method called SetAttributeFilter ( ) To filter out the Feature that meets a certain condition in the Layer. After the Filter is set, you can use the GetNextFeature () method to retrieve the qualified Feature in sequence. SetAttributeFilter (None) can identify a Filter. For example

>>> Layer. GetFeatureCount ()

42

>>> Layer. SetAttributeFilter ("cover = 'shrubs '")

>>> Layer. GetFeatureCount ()

6

>>> Layer. SetAttributeFilter (None)

>>> Layer. GetFeatureCount ()

42

Space filter Spatial filters

There are two types. One is SetSpatialFilter ( ) To filter a type of Feature. for example, if you enter Polygon in the parameter, all the Polygon

In addition, SetSpatialFilterRect ( , , , ). Input four coordinates to select the Feature in the box.

SetSpatialFilter (None) is the same as clearing the space attribute filter.

For example, in the following code, layerAreas is polygon and layerSites is point

>>> FeatAreas = layerAreas. GetNextFeature ()

>>> Poly = featAreas. GetGeometryRef ()

>>> LayerSites. GetFeatureCount ()

42

>>> LayerSites. SetSpatialFilter (poly)

>>> LayerSites. GetFeatureCount () >>> layerSites. GetFeatureCount ()

33

>>> LayerSites. SetSpatialFilterRect (460000,459 0000, 490000,460 0000)

>>> LayerSites. GetFeatureCount ()

4

>>> LayerSites. SetSpatialFilter (None)

>>> LayerSites. GetFeatureCount ()

42

More complex filters, such as executing the SQL query statement ExecuteSQL ( With the powerful functions of SQL, you can execute more complex tasks. for example, in the following code, select the Feature of the cover type as grass and sort it in descending order by ID.

Result = dsSites. ExecuteSQL ("select * from sites where cover = 'grass 'order by id desc ")

ResultFeat = result. GetNextFeature ()

While resultFeat:

Print resultFeat. GetField ('id') print resultFeat. GetField ('id ')

ResultFeat = result. GetNextFeature ()

DsSites. ReleaseResultSet (result)

42

40

:

4

Last sentence ReleaseResultSet ( ) Is to release the query results, must be released before executing the next SQL statement.

The following example calculates the number of all Feature records whose cover is grass.

>>> Result = dsSites. ExecuteSQL ("select count (*) from sites where cover = 'grass '")

>>> Result. GetFeatureCount ()

11

>>> Result. GetFeature (0). GetField (0)

11

>>> DsSites. ReleaseResultSet (result)

List all different cover types

Result = ds. ExecuteSQL ("select distinct cover from sites ")

ResultFeat = result. GetNextFeature ()

While resultFeat:

Print resultFeat. GetField (0)

ResultFeat = result. GetNextFeature ()

Ds. ReleaseResultSet (result)

Shrubs

Trees

Rocks

Grass

Bare

Water

Count how many Feature each cover type has

CoverLayer = ds. ExecuteSQL ('select distinct cover from sites ')

CoverFeat = coverLayer. GetNextFeature ()

While coverFeat:

CntLayer = ds. ExecuteSQL ("select count (*) from sites where cover = '" + coverFeat. GetField (0) + "'")

Print coverFeat. GetField (0) + ''+ print coverFeat. GetField (0) +'' + cntLayer. GetFeature (0). GetFieldAsString (0)

Ds. ReleaseResultSet (cntLayer)

CoverFeat = coverLayer. GetNextFeature ()

Ds. ReleaseResultSet (coverLayer)

Shrubs 6

Trees 11

Rocks 6

Grass 11

Bare 6

Water 2

Intersect checks whether two elements are at the same intersection

Poly2.Intersect (poly1)

Returns 0 to indicate non-intersection, and 1 to indicate intersection.

Disjoint determines whether two elements are not intersecting
Poly2.Disjoint (poly1)

Return 1 indicates not intersection, return 0 indicates intersection, exactly the opposite of Intersect

Touch indicates adjacent (edge)

Poly2.Touches (poly1)

If the return value is 0, the edge is not wiped. if the return value is 1, The Edge is wiped.

Crosses traversal. generally, a line passes through a polygon.

Poly2.Crosses (line)

If the return value is 0, the traversal is not performed. if the return value is 1, the traversal is performed.

Within contains, and one element is completely circled by another element.

PtB. Within (poly1)

0 indicates that the point is outside the polygon. 1 indicates that the point is inside the polygon.

Contains, which is the opposite of.

Poly1.Contains (ptB)

Just change the main call object and parameters.

Overlaps overlap. it seems that only two polygon can overlap

Poly2.Overlaps (poly3)

0 indicates no overlap, and 1 indicates overlap.

Next let's take a look at the simple geographic data processing geoprocessing

Polygon:

Intersection: poly3.Intersection (poly2)

And: poly3.Union (poly2)

Difference: poly3.Difference (poly2)

Supplement: poly3.20.ricdifference (poly2)

Geometry:

. Buffer ( Add a buffer to geometry to convert the Point line into a polygon.

. Equal ( ) Are two geometry equal?

. Distance ( Returns the shortest distance between two Ry.

The. GetEnvelope () envelope is interesting. In fact, it is to frame the geometric shape with a square frame and return the coordinates of the Four Corners (minx, maxx, miny, maxy)

Python function, exception and module can be found in any python textbook.

The above is the python gdal Tutorial: filter, simple Space Analysis, functions and modules. For more information, see PHP Chinese website (www.php1.cn )!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.