Layer, featureclass, etc (OGR, originally speaking of OGR, not ESRI products)

Source: Internet
Author: User
Tags gety in degrees

You need to explain the Layer. The Layer here refers to (Feature". It is equivalent to FeatureClass in the model defined by ESRI, or FeatureDataSet ). In short, it is a collection of elements. Modeling our world:

A feature dataset (feature set) is a set of feature classes with the same coordinate system. We can choose to organize simple element classes within or outside the element set, but the topological element classes can only be organized within the element set to ensure that they have the same coordinate system.

The element classes are described as follows:

A feature class is a collection of elements with the same geometric shape: points, lines, or polygon. The two most important elements are simple elements and Topological elements.

Simple Element classes include no topologyLinkPoint, line, polygon, or note. That is to say, the points in one element class can be the same as the line endpoints in the other element class, but they are different. These elements can be edited independently.

The topology element class is limited to a certain graphic range. It is a set of elements restricted by a complete topology unit.

As you can see, Layer core operations are all for element operations (FeatureGetFeatureCount, GetFeature, or GetNextFeature can be used to obtain all the elements in the layer. GetFeaturesRead can be used to know how many items have been read.Feature(1 less value is obtained at a time. If you want to start from scratch, use ResetReading ). So what are elements?

 

In fact, elements are some shapes. The specific score is divided into points, lines, polygon, arc segments, vectors, control points, and so on. No matter how many nodes are, let's take it into consideration. Element classes generally contain an Attribute Table (element + attribute is used to form GIS. The so-called GIS spatial query is the correspondence between elements and attributes, and the elements selected according to the condition correspond to the attributes, the selected Attribute corresponds to the element ). A element generally corresponds to a row in a property table.

 

1. Introduction to OGR

OGR is a library for reading and processing GSI vector data. This library can read and process a variety of popular vector data (for details about what data can be processed, see here. Note that some data can only be read but cannot be written ).

2. Installation

OGR is a part of the GDAL library. As long as you have installed the GDAL library, you already have the OGR library. For how to install GDAL, see here.

3. Quick Start

3.1. Open a Vector Data Source

The first step is to see how to open a data. One common dataset of Vector Data is ESRI's shapefile and the other is MapInfo's TAB. They both contain several files, and a single copy of a file cannot be opened. However, you only need to open the most important files in OGR. In the shapefile, this file ends with. shp. In TAB, this file ends with. tab.

Next we open a ShapeFile file. This file is the Country data in the standard tutorial data. It is actually the most common map of the world.

3.2. Data Source, DataSource Toggle line numbers

   1 >>> import ogr
2 >>> dir(ogr)
3 ['BuildPolygonFromEdges', 'CreateGeometryFromGML', 'CreateGeometryFromWkb', 'Cre
4 ateGeometryFromWkt', 'DataSource', 'Driver', 'Feature', 'FeatureDefn', 'FieldDef
5 n', 'Geometry', 'GetDriver', 'GetDriverByName', 'GetDriverCount', 'GetOpenDS', '
6 GetOpenDSCount', 'Layer', 'ODrCCreateDataSource', 'ODrCDeleteDataSource', 'ODsCC
7 reateLayer', 'ODsCDeleteLayer', 'OFTBinary', 'OFTInteger', 'OFTIntegerList', 'OF
8 TReal', 'OFTRealList', 'OFTString', 'OFTStringList', 'OFTWideString', 'OFTWideSt
9 ringList', 'OGRError', 'OJLeft', 'OJRight', 'OJUndefined', 'OLCCreateField', 'OL
10 CDeleteFeature', 'OLCFastFeatureCount', 'OLCFastGetExtent', 'OLCFastSetNextByInd
11 ex', 'OLCFastSpatialFilter', 'OLCRandomRead', 'OLCRandomWrite', 'OLCSequentialWr
12 ite', 'OLCTransactions', 'Open', 'OpenShared', 'SetGenerate_DB2_V72_BYTE_ORDER',
13 '__builtins__', '__doc__', '__file__', '__name__', '_gdal', 'gdal', 'osr', 'ptr
14 add', 'ptrcast', 'ptrcreate', 'ptrfree', 'ptrmap', 'ptrptrcreate', 'ptrptrset',
15 'ptrptrvalue', 'ptrset', 'ptrvalue', 'sys', 'types', 'wkb25Bit', 'wkbGeometryCol
16 lection', 'wkbGeometryCollection25D', 'wkbLineString', 'wkbLineString25D', 'wkbL
17 inearRing', 'wkbMultiLineString', 'wkbMultiLineString25D', 'wkbMultiPoint', 'wkb
18 MultiPoint25D', 'wkbMultiPolygon', 'wkbMultiPolygon25D', 'wkbNDR', 'wkbNone', 'w
19 kbPoint', 'wkbPoint25D', 'wkbPolygon', 'wkbPolygon25D', 'wkbUnknown', 'wkbXDR']
20 >>> datasource = ogr.Open("e:/gisdata/data/country.shp")

In this way, a data source is opened ).

Let's see what's in this dataSource.

Toggle line numbers

   1 >>> dir(datasource)
2 ['CopyLayer', 'CreateLayer', 'DeleteLayer', 'Dereference', 'Destroy', 'ExecuteSQ
3 L', 'GetDriver', 'GetLayer', 'GetLayerByName', 'GetLayerCount', 'GetName', 'GetR
4 efCount', 'GetSummaryRefCount', 'Reference', 'Release', 'ReleaseResultSet', 'Tes
5 tCapability', '__doc__', '__getitem__', '__init__', '__len__', '__module__', '_o
6 ', 'driver_o']
7 >>>

In the beginning, we only look at Get functions. Here, we can only operate on Layer.

3.3. Layer, Layer

You need to explain the Layer. The Layer here refers to (Feature". It is equivalent to FeatureClass in the model defined by ESRI, or FeatureDataSet ). In short, it is a collection of elements. Modeling our world:

A feature dataset (feature set) is a set of feature classes with the same coordinate system. We can choose to organize simple element classes within or outside the element set, but the topological element classes can only be organized within the element set to ensure that they have the same coordinate system.

The element classes are described as follows:

A feature class is a collection of elements with the same geometric shape: points, lines, or polygon. The two most important elements are simple elements and Topological elements.

Simple Element classes include no topologyLinkPoint, line, polygon, or note. That is to say, the points in one element class can be the same as the line endpoints in the other element class, but they are different. These elements can be edited independently.

The topology element class is limited to a certain graphic range. It is a set of elements restricted by a complete topology unit.

The following is a set of Layer methods.

Toggle line numbers

   1 >>> layer = datasource.GetLayer(0)
2 >>> dir(layer)
3 ['CommitTransaction', 'CreateFeature', 'CreateField', 'DeleteFeature', 'Derefere
4 nce', 'GetExtent', 'GetFeature', 'GetFeatureCount', 'GetFeaturesRead', 'GetLayer
5 Defn', 'GetName', 'GetNextFeature', 'GetRefCount', 'GetSpatialFilter', 'GetSpati
6 alRef', 'Reference', 'ResetReading', 'RollbackTransaction', 'SetAttributeFilter'
7 , 'SetFeature', 'SetNextByIndex', 'SetSpatialFilter', 'SetSpatialFilterRect', 'S
8 tartTransaction', 'SyncToDisk', 'TestCapability', '__doc__', '__init__', '__len_
9 _', '__module__', '_o']
10 >>>

As you can see, Layer core operations are all for element operations (FeatureGetFeatureCount, GetFeature, or GetNextFeature can be used to obtain all the elements in the layer. GetFeaturesRead can be used to know how many items have been read.Feature(1 less value is obtained at a time. If you want to start from scratch, use ResetReading ). So what are elements?

3.4. elements, Feature

In fact, elements are some shapes. The specific score is divided into points, lines, polygon, arc segments, vectors, control points, and so on. No matter how many nodes are, let's take it into consideration. Element classes generally contain an Attribute Table (element + attribute is used to form GIS. The so-called GIS spatial query is the correspondence between elements and attributes, and the elements selected according to the condition correspond to the attributes, the selected Attribute corresponds to the element ). An element generally corresponds to a row in the Attribute Table.

Capture oneFeatureLet's see ......

Toggle line numbers

   1 >>> feature = layer.GetFeature(0)
2 >>> dir(feature)
3 ['Clone', 'Destroy', 'DumpReadable', 'Equal', 'GetDefnRef', 'GetFID', 'GetField'
4 , 'GetFieldAsDouble', 'GetFieldAsInteger', 'GetFieldAsString', 'GetFieldCount',
5 'GetFieldDefnRef', 'GetFieldIndex', 'GetGeometryRef', 'GetStyleString', 'IsField
6 Set', 'SetFID', 'SetField', 'SetFrom', 'SetGeometry', 'SetGeometryDirectly', 'Se
7 tStyleString', 'UnsetField', '__cmp__', '__copy__', '__del__', '__doc__', '__get
8 attr__', '__init__', '__module__', '__setattr__', '_o', 'thisown']

You can see the operations on Field and Geometry.Feature. Field is related to attribute operations. Geometry is related to shape operations.

Toggle line numbers

   1 >>> for i in range(feature.GetFieldCount()):
2 ... print feature.GetField(i)
3 ...
4 AA
5 ABW
6 Aruba
7 Netherlands
8 67074
9 182.926
10 70.628
11 Florin
12 AWG
13 N
14 1

This isFeatureIf you want to view the structure of the entire table, the names of each field, and so on, you cannotFeature. See the additional information of the layer.

Toggle line numbers

   1 >>> layerdef = layer.GetLayerDefn()
2 >>> for i in range(layerdef.GetFieldCount()):
3 ... defn = layerdef.GetFieldDefn(i)
4 ... print defn.GetName(),defn.GetWidth(),defn.GetType(),defn.GetPrecision()
5 ...
6 FIPS_CNTRY 2 4 0
7 GMI_CNTRY 3 4 0
8 CNTRY_NAME 40 4 0
9 SOVEREIGN 40 4 0
10 POP_CNTRY 10 0 0
11 SQKM_CNTRY 12 2 3
12 SQMI_CNTRY 12 2 3
13 CURR_TYPE 16 4 0
14 CURR_CODE 4 4 0
15 LANDLOCKED 1 4 0
16 COLOR_MAP 1 4 0
17 >>>

This is the entire table structure. Of course, the type is enumeration. It depends on the Data Type name and you need to create a data type dictionary. I will not write it here.

3.5. Shape, Geometry Toggle line numbers

   1 >>> geom = feature.GetGeometryRef()
2 >>> dir(geom)
3 ['AddGeometry', 'AddGeometryDirectly', 'AddPoint', 'AddPoint_2D', 'AssignSpatial
4 Reference', 'Buffer', 'Centroid', 'Clone', 'CloseRings', 'Contains', 'ConvexHull
5 ', 'Crosses', 'Destroy', 'Difference', 'Disjoint', 'Empty', 'Equal', 'ExportToGM
6 L', 'ExportToWkb', 'ExportToWkt', 'FlattenTo2D', 'GetArea', 'GetBoundary', 'GetC
7 oordinateDimension', 'GetDimension', 'GetEnvelope', 'GetGeometryCount', 'GetGeom
8 etryName', 'GetGeometryRef', 'GetGeometryType', 'GetPointCount', 'GetSpatialRefe
9 rence', 'GetX', 'GetY', 'GetZ', 'Intersect', 'Intersection', 'Overlaps', 'SetCoo
10 rdinateDimension', 'SetPoint', 'SetPoint_2D', 'SymmetricDifference', 'Touches',
11 'Transform', 'TransformTo', 'Union', 'Within', 'WkbSize', '__copy__', '__del__',
12 '__doc__', '__init__', '__module__', '__str__', '_o', 'thisown']
13 >>> geom.GetGeometryName()
14 'POLYGON'
15 >>> geom.GetGeometryCount()
16 1
17 >>> geom.GetPointCount()
18 0
19 >>> polygon = geom.GetGeometryRef(0)
20 >>> polygon.GetGeometryName()
21 'LINEARRING'
22 >>> polygon.GetGeometryCount()
23 0
24 >>> polygon.GetPointCount()
25 11
26 >>> polygon.GetX(0)
27 -69.882232666015625
28 >>> polygon.GetY(0)
29 12.411109924316406
30 >>> polygon.GetZ(0)
31 0.0
32 >>>

This is the main operation on Geometry (you can also try the ExportToWkt method to get the Wkt representation of the whole polygon and see all the coordinate points ). These operations can already draw the National outlines of Aruba through a loop (it should not be against the law to paste the data here ~, This is the standard Demo data ). Note that if the Geometry type is polygon, there will be two layers of Geometry. After obtaining the polygon, you also need to obtain the line that forms the polygon edge to obtain the points that constitute the polygon. If the type is single line or point, you can use GetX and GetY directly, instead of taking another step to GetGeometryRef to obtain the child shape.

Note that these vertices are coordinate in the geographical sense, rather than coordinate in the data meaning. The Unit is determined based on the spatial reference (this data should use the longitude and latitude, rather than the projection coordinate unit meter ). After obtaining these coordinates, you do not need to calculate the geographical coordinates based on the surrounding boundary points like the Raster Data (The Raster Data has the length and width of the object and the row and column coordinates in the Data sense, the vector does not actually indicate the length and width of the data before it is attached to an actual object surface, such as a computer screen or printed paper. Therefore, for vector data, it doesn't make sense if there is no painting operation.) It should be directly spread to a defined projection or geographic coordinate system on an actual plane.

Vector and raster datasets have spatial reference information. In addition, the spatial reference information is directly infiltrated into eachFeature. Because the coordinate system and the affine parameters of the ShapeFile are separated. The coordinate system is obtained by attaching the. prj file to the same directory of the shp file. But the affine parameter is directly placed inside the ShapeFile file, but it is different from the tfw of GeoTiff, and its representation is an outsourcing rectangle. Record the geographic range of the entire layer. Because I cannot find the prj file, I cannot read the space reference here. However, the geographical range is acceptable.

Toggle line numbers

   1 >>> layer.GetSpatialRef()
2 >>> layer.GetExtent()
3 (-180.0, 180.0, -90.0, 83.62359619140625)
4 >>>

Except that the entire layer has a geographical rangeFeatureIt also has a geographical range. However, this geographic range is stored in GeometryDef.

Toggle line numbers

   1 >>> geom.GetEnvelope()
2 (-70.059661865234375, -69.874862670898437, 12.411109924316406, 12.627776145935059)
3 >>> geom.GetSpatialReference()
4 >>>

In this case, the obtained outsourcing rectangle is only for this element, not the entire layer. The coordinate system here cannot be obtained. The obtained information should be the same as that of the Layer.

There are many strange methods in Geometry, such as Buffer, Contains, Crosses, Disjoint, Overlaps, and Union. These are all geometricLinkCalculation, which involves the scope of spatial analysis.LinkThe operation can be discussed separately (ryLinkComputing usually requires the support of another database GEOS. Otherwise, OGR will not be able to perform operations normally. This topic will be discussed later ).

There is a good method. That is, the GetArea () method can obtain the area surrounded by the whole polygon. This method was not found in the C ++ documentation, but in Python (Oh ~~ God loves Python ~~).

Toggle line numbers

   1 >>> geom.GetArea()
2 0.016598101236013463
3 >>>

However, this is measured in degrees. It has lost its significance as an area. For accurate area, you can convert the area from degree to meter based on the spatial reference, and then calculate the square meter. However, my prj projection file is missing and there is no way to do it.

4. Summary

We can see that the entire ogr structure is clear, and each part is standard. The first is the data source. Open the data source with a series of layers (whether it is a vector dataset or a feature class), the layers are the elements that represent the ground map under many references of the same space (the elements are the abstraction of the ground map, for specific abstract expressions, refer to this introduction ). Elements include the geometric shapes and associated attributes under the same space reference. Ry consists of several subshapes or a series of points, which represent their respective geographical locations. In this way, the whole vector model is completely split. The process of creating vector data is the inverse process of the above process.

5. Feedback

If you find that there is a problem with what I wrote or you have any comments on what I wrote, please log on to this forum.

 

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.