Geometry Fields

Geometry fields are distinguished from other fields. They are defined in ogr by the class osgeo.ogr.Geometry, which has the following methods:

  • Methods returning a pandas Series or DataFrame:

    • ExportToGML, ExportToIsoWkb, ExportToIsoWkt, ExportToJson, ExportToKML, ExportToWkb, ExportToWkt
    • GetArea, GetBoundary, GetCoordinateDimension, GetCurveGeometry, GetDimension, GetEnvelope, GetEnvelope3D, GetGeometryCount, GetGeometryName, GetGeometryRef, GetGeometryType, GetLinearGeometry, GetM, GetPoint, GetPointCount, GetPointZM, GetPoint_2D, GetPoints, GetSpatialReference, GetX, GetY, GetZ, WkbSize
    • Is3D, IsEmpty, IsMeasured, IsRing, IsSimple, IsValid
  • Methods returning a layer with attributes:

    • ConvexHull, Buffer, Centroid, DelaunayTriangulation, Simplify, SimplifyPreserveTopology
    • Contains, Crosses, Disjoint, Empty, Equal, Equals, Intersects, Overlaps, Touches, Within
    • Difference, Intersect, Intersection, Segmentize, SymDifference, SymmetricDifference, Union
    • Area, Boundary, CloseRings, CoordinateDimension, Destroy, Distance, Distance3D, FlattenTo2D, HasCurveGeometry, Length, PointOnSurface, Transform, TransformTo, UnionCascaded, Value
  • Methods requiring LayersUpdate or LayersWrite:

    • AssignSpatialReference
    • AddGeometry, AddGeometryDirectly, AddPoint, AddPointM, AddPointZM, AddPoint_2D
    • Set3D, SetCoordinateDimension, SetMeasured, SetPoint, SetPointM, SetPointZM, SetPoint_2D

Note

Geometry methods return pandas Series or pandas DataFrame.

Export geometries

Geometries are exported as pandas DataFrame in the formats gml, isowkb, isowkt, json, kml, wkb, and wkt.

from girs.feat.layers import LayersReader
lrs = LayersReader('D:/tmp/girs/DEU_adm_shp/DEU_adm4.shp')
print lrs.geometries_export()  #  equivalent to fmt='wkt', layer_number=0

Output:

FID
0        MULTIPOLYGON (((9.75168800354004 48.3219490051...
1        MULTIPOLYGON (((9.74722671508795 48.3244895935...
2        POLYGON ((9.82752799987793 48.4570274353028,9....
                               ...
11299    POLYGON ((11.2021255493163 51.0652885437012,11...
11300    POLYGON ((11.3888244628907 51.0245742797852,11...
11301    POLYGON ((11.2615299224854 50.9376220703126,11...
Name: GEOM, Length: 11302, dtype: object

The example below compares data export in wkt and wkb. The latter is ca. 30 times faster [1] and requires ca. half storage size.

t0 = time.time()
df_wkt = lrs.geometries_export(fmt='wkt')
print 'WKT geometries: {dt} seconds'.format(dt=time.time()-t0)

t0 = time.time()
df_wkb = lrs.geometries_export(fmt='wkb')
print 'WKB geometries: {dt} seconds'.format(dt=time.time()-t0)

wkt_length = df_wkt.apply(lambda g: len(g))  # get the length of each wkt geometry string
wkb_length = df_wkb.apply(lambda g: len(g))  # get the length of each wkb geometry string

df = pd.concat([wkt_length, wkb_length, wkt_length/wkb_length], axis=1)
df.columns = ['wkt length', 'wkb length', 'length wkt/wkb']

print 'Lengths mean values:\n{}'.format(df.mean())
print 'Lengths standard deviations:\n{}'.format(df.std())

Output:

WKT geometries: 5.21000003815 seconds
WKB geometries: 0.160000085831 seconds
Lengths mean values:
wkt length        5795.756415
wkb length        2755.171474
length wkt/wkb       2.099449
dtype: float64
Lengths standard deviations:
wkt length        4935.985512
wkb length        2340.461718
length wkt/wkb       0.010039
dtype: float64

Geometry count

from girs.feat.layers import LayersReader
lrs = LayersReader('D:/tmp/girs/DEU_adm_shp/DEU_adm4.shp')
print lrs.get_geometries_count()

Output:

FID
0        2
1        2
2        1
        ..
11299    1
11300    1
11301    1
Name: geom. count, Length: 11302, dtype: int64

Area

from girs.feat.layers import LayersReader
lrs = LayersReader('D:/tmp/girs/DEU_adm_shp/DEU_adm4.shp')
df = lrs.get_geometries_areas(data_frame=True)
print df
print 'Total area: {:.5}'.format(df['area'].sum())

Output:

           area
FID
0        0.005590
1        0.000936
           ...
11299    0.001332
11300    0.000522
11301    0.010805

[11302 rows x 1 columns]

Total area: 45.92

Boundaries

from girs.feat.layers import LayersReader
lrs = LayersReader('D:/tmp/girs/DEU_adm_shp/DEU_adm4.shp')
print lrs.get_geometries_boundaries(fmt='wkt')

Output:

FID
0        MULTILINESTRING ((9.75168800354004 48.32194900...
1        MULTILINESTRING ((9.74722671508795 48.32448959...
2        LINESTRING (9.82752799987793 48.4570274353028,...
                               ...
11299    LINESTRING (11.2021255493163 51.0652885437012,...
11300    LINESTRING (11.3888244628907 51.0245742797852,...
11301    LINESTRING (11.2615299224854 50.9376220703126,...
Name: boundary, Length: 11302, dtype: object

Centroids

from girs.feat.layers import LayersReader
lrs = LayersReader('D:/tmp/girs/DEU_adm_shp/DEU_adm4.shp')
print lrs.get_geometries_centroids()

Output:

        x_center   y_center
FID
0       9.708450  48.333522
1       9.772435  48.331778
2       9.771710  48.464195
...          ...        ...
11299  11.211392  51.085274
11300  11.372233  51.028024
11301  11.317872  50.978347

[11302 rows x 2 columns]

Coordinate dimensions

from girs.feat.layers import LayersReader
lrs = LayersReader('D:/tmp/girs/DEU_adm_shp/DEU_adm4.shp')
print lrs.get_geometries_coordinate_dimensions().unique()

Curve geometry

from girs.feat.layers import LayersReader
lrs = LayersReader('D:/tmp/girs/DEU_adm_shp/DEU_adm4.shp')
print lrs.get_curve_geometries()

Dimensions

from girs.feat.layers import LayersReader
lrs = LayersReader('D:/tmp/girs/DEU_adm_shp/DEU_adm4.shp')
print lrs.get_geometries_dimensions().unique()

Envelopes

Envelope 2D

from girs.feat.layers import LayersReader
lrs = LayersReader('D:/tmp/girs/DEU_adm_shp/DEU_adm4.shp')
print lrs.get_geometries_envelopes()

Output:

            xmin       xmax       ymin       ymax
0       9.618460   9.822202  48.298607  48.365326
1       9.743726   9.800255  48.319130  48.347599
2       9.716778   9.827528  48.433918  48.487774
...          ...        ...        ...        ...
11299  11.192033  11.231587  51.063351  51.104897
11300  11.351975  11.394562  51.018730  51.037430
11301  11.234882  11.402000  50.916714  51.036720

[11302 rows x 4 columns]

Envelope 3D

from girs.feat.layers import LayersReader
lrs = LayersReader('D:/tmp/girs/DEU_adm_shp/DEU_adm4.shp')
print lrs.get_geometries_envelopes3D()

Output:

            xmin       xmax       ymin       ymax  zmin  zmax
0       9.618460   9.822202  48.298607  48.365326   0.0   0.0
1       9.743726   9.800255  48.319130  48.347599   0.0   0.0
2       9.716778   9.827528  48.433918  48.487774   0.0   0.0
...          ...        ...        ...        ...   ...   ...
11299  11.192033  11.231587  51.063351  51.104897   0.0   0.0
11300  11.351975  11.394562  51.018730  51.037430   0.0   0.0
11301  11.234882  11.402000  50.916714  51.036720   0.0   0.0

[11302 rows x 6 columns]

Names

from girs.feat.layers import LayersReader
lrs = LayersReader('D:/tmp/girs/DEU_adm_shp/DEU_adm4.shp')
print lrs.get_geometries_names()

Output:

FID
0        MULTIPOLYGON
1        MULTIPOLYGON
2             POLYGON
             ...

11299         POLYGON
11300         POLYGON
11301         POLYGON
Name: geom_name, Length: 11302, dtype: object

Linear geometries

from girs.feat.layers import LayersReader
lrs = LayersReader('D:/tmp/girs/DEU_adm_shp/DEU_adm4.shp')
print lrs.get_linear_geometries()

X, Y, Z, and M

from girs.feat.layers import LayersRea    from girs.feat.layers import LayersReader
lrs0 = LayersReader('D:/tmp/girs/DEU_adm_shp/DEU_adm4.shp')
lrs1 = lrs0.centroid()
print pd.concat([
    lrs1.get_geometries_x(),
    lrs1.get_geometries_y(),
    lrs1.get_geometries_z(),
    lrs1.get_geometries_m()], axis=1)

Output:

               x          y    z    m
FID
0       9.708450  48.333522  0.0  0.0
1       9.772435  48.331778  0.0  0.0
2       9.771710  48.464195  0.0  0.0
...          ...        ...  ...  ...
11299  11.211392  51.085274  0.0  0.0
11300  11.372233  51.028024  0.0  0.0
11301  11.317872  50.978347  0.0  0.0

[11302 rows x 4 columns]

Points 2D

from girs.feat.layers import LayersReader
lrs0 = LayersReader('D:/tmp/girs/DEU_adm_shp/DEU_adm4.shp')
lrs1 = lrs0.centroid()
print lrs1.get_geometries_points_2d()

Output:

               x          y
FID
0       9.708450  48.333522
1       9.772435  48.331778
...          ...        ...
11299  11.211392  51.085274
11300  11.372233  51.028024
11301  11.317872  50.978347

[11302 rows x 2 columns]

Points

from girs.feat.layers import LayersReader
lrs0 = LayersReader('D:/tmp/girs/DEU_adm_shp/DEU_adm4.shp')
lrs1 = lrs0.centroid()
print lrs1.get_geometries_points()

Output:

               x          y    z
FID
0       9.708450  48.333522  0.0
1       9.772435  48.331778  0.0
2       9.771710  48.464195  0.0
...          ...        ...  ...
11299  11.211392  51.085274  0.0
11300  11.372233  51.028024  0.0
11301  11.317872  50.978347  0.0

[11302 rows x 3 columns]

Points ZM

from girs.feat.layers import LayersReader
lrs0 = LayersReader('D:/tmp/girs/DEU_adm_shp/DEU_adm4.shp')
lrs1 = lrs0.centroid()
print lrs1.get_geometries_points_zm()

Output:

               x          y    z    m
FID
0       9.708450  48.333522  0.0  0.0
1       9.772435  48.331778  0.0  0.0
2       9.771710  48.464195  0.0  0.0
...          ...        ...  ...  ...
11299  11.211392  51.085274  0.0  0.0
11300  11.372233  51.028024  0.0  0.0
11301  11.317872  50.978347  0.0  0.0

Spatial references

from girs.feat.layers import LayersReader
lrs0 = LayersReader('D:/tmp/girs/DEU_adm_shp/DEU_adm4.shp')
print lrs0.get_geometries_spatial_references()

WKB sizes

from girs.feat.layers import LayersReader
lrs0 = LayersReader('D:/tmp/girs/DEU_adm_shp/DEU_adm4.shp')
print lrs0.get_geometries_wkb_size()

Is geometry 3D

from girs.feat.layers import LayersReader
lrs0 = LayersReader('D:/tmp/girs/DEU_adm_shp/DEU_adm4.shp')
print lrs0.is_geometry_3d().value_counts()

Is geometry empty

from girs.feat.layers import LayersReader
lrs0 = LayersReader('D:/tmp/girs/DEU_adm_shp/DEU_adm4.shp')
print lrs0.is_geometry_empty().value_counts()

Is geometry measured

from girs.feat.layers import LayersReader
lrs0 = LayersReader('D:/tmp/girs/DEU_adm_shp/DEU_adm4.shp')
print lrs0.is_geometry_measured().value_counts()

Is geometry point

from girs.feat.layers import LayersReader
lrs0 = LayersReader('D:/tmp/girs/DEU_adm_shp/DEU_adm4.shp')
print lrs0.is_geometry_point()

Is geometry polygon

from girs.feat.layers import LayersReader
lrs0 = LayersReader('D:/tmp/girs/DEU_adm_shp/DEU_adm4.shp')
print lrs0.is_geometry_polygon().value_counts()

Is geometry ring

from girs.feat.layers import LayersReader
lrs0 = LayersReader('D:/tmp/girs/DEU_adm_shp/DEU_adm4.shp')
print lrs0.is_geometry_ring().value_counts()

Is geometry simple

from girs.feat.layers import LayersReader
lrs0 = LayersReader('D:/tmp/girs/DEU_adm_shp/DEU_adm4.shp')
print lrs0.is_geometry_simple().value_counts()

Footnotes

[1]time.time() is an easy but not most appropriate way to measure cpu time.