Editing layers

Deleting a field requires an instance of LayersUpdate or LayersWriter. If these instances are not in the memory, the source will be overwritten.

LayersEditor instance

Writing layers

Creating a LayersSet with one layer in memory (RAM). The layer has neither coordinate system nor user defined fields.

from girs.feat.layers import LayersWriter
lrs = LayersWriter(SomeLayerName_0=[ogr.wkbPolygon], SomeLayerName_1=[ogr.wkbPolygon])
print lrs.get_description()
SomeLayerName

Creating a shapefile (one layer) without coordinate system and without user defined fields. This is not conform to ESRI ArcMap, which requires at least one user defined attribute.

lrs = LayersWriter('D:/tmp/some_shapefile.shp', layer_1=[ogr.wkbPolygon])
print lrs.get_description()
some_shapefile

Creating a shapefile without coordinate system and with two user defined fields.

fields = [FieldDefinition("Name", ogr.OFTString), FieldDefinition("Area", ogr.OFTReal)]
lrs = LayersWriter('D:/tmp/another_shapefile.shp', layer=[ogr.wkbPolygon, None, fields])
print lrs.get_field_names()
['Name', 'Area']

Creating a shapefile with coordinate system and with two user defined fields. See http://spatialreference.org/ for coordinate systems.

from osgeo import ogr
from girs.feat.layers import LayersWriter, FieldDefinition
from girs.srs import get_srs
fields = [FieldDefinition("Name", ogr.OFTString), FieldDefinition("Area", ogr.OFTReal)]
lrs = LayersWriter('D:/tmp/girs/studyarea.shp',
                    layer=[ogr.wkbPolygon, get_srs(epsg=4326), fields])

Inserting data into the layer passing a WKT geometry.

from girs.feat.geom import create_polygon
wkt = create_polygon([(6.77, 50.83), (7.16, 50.83), (7.16, 51.08),
                      (6.77, 51.08), (6.77, 50.83)]).ExportToWkt()
lrs.create_feature(geom=wkt, layer_number=0, Name=u'Köln', Area=0.0)
lrs.show()
      geom  Name  Area
0  Polygon  Köln   0.0

Inserting data into the layer passing an ogr geometry obtained from the utilities in girs.feat.geom.

from osgeo import ogr
from girs.feat.geom import create_polygon
from girs.feat.layers import LayersWriter, FieldDefinition
from girs.srs import get_srs
fields = [FieldDefinition("Name", ogr.OFTString), FieldDefinition("Area", ogr.OFTReal)]
lrs = LayersWriter('D:/tmp/girs/studyarea1.shp',
                    layer=[ogr.wkbPolygon, get_srs(epsg=4326), fields])
g = create_polygon([(6.77, 50.83), (7.16, 50.83), (7.16, 51.08), (6.77, 51.08), (6.77, 50.83)])
lrs.create_feature(geom=g, layer_number=0, Name=u'Düsseldorf', Area=10.0)
lrs.show()
      geom        Name  Area
0  Polygon  Düsseldorf  10.0

Creating a GML file with two layers:

from osgeo import ogr
from girs.feat.layers import LayersWriter, FieldDefinition
from girs.srs import get_srs
fields0 = [FieldDefinition("Name", ogr.OFTString), FieldDefinition("Area", ogr.OFTReal)]
fields1 = [FieldDefinition("Code", ogr.OFTInteger), FieldDefinition("Capital", ogr.OFTString)]
lrs = LayersWriter('D:/tmp/girs/studyarea.gml', 'GML',
                   name_0=[ogr.wkbPolygon, get_srs(epsg=4326), fields0],
                   name_1=[ogr.wkbPolygon, get_srs(epsg=4326), fields1])
print lrs.get_description(0), lrs.get_field_names(0)
print lrs.get_description(1), lrs.get_field_names(1)

Outputs:

name0 ['Name', 'Area']
name1 ['Code', 'Capital']

Update instance

from girs.feat.layers import LayersUpdate
lrs0 = LayersUpdate('D:/tmp/girs/DEU_adm_shp/DEU_adm4_copy.shp')

Delete fields

as in the example below:

from girs.feat.layers import LayersUpdate

lrs0 = LayersUpdate('D:/tmp/girs/DEU_adm_shp/DEU_adm4_copy.shp')
print 'Fields before:', lrs0.get_field_names()
lrs0.delete_fields(['ID_0', 'ISO', 'NAME_0', 'ID_1', 'ID_2', 'ID_3', 'ID_4',
                    'VARNAME_4', 'CCN_4', 'CCA_4', 'TYPE_4', 'ENGTYPE_4'])
print 'Fields after:', lrs0.get_field_names()

Output:

Fields before: ID_0, ISO, NAME_0, ID_1, NAME_1, ID_2, NAME_2, ID_3, NAME_3, ID_4, NAME_4, VARNAME_4, CCN_4, CCA_4, TYPE_4, ENGTYPE_4
Fields after: NAME_1, NAME_2, NAME_3, NAME_4

The original source can be copied before deleting fields:

from girs.feat.layers import LayersUpdate
lrs0 = LayersReader('D:/tmp/girs/DEU_adm_shp/DEU_adm4.shp').copy()  # copy on memory
lrs0.delete_fields(['ID_0', 'ISO', 'NAME_0', 'ID_1', 'ID_2', 'ID_3', 'ID_4',
                    'VARNAME_4', 'CCN_4', 'CCA_4', 'TYPE_4', 'ENGTYPE_4'])
print 'Fields:', lrs0.get_field_names()

Calculating area

import ogr
from girs.feat.layers import FieldDefinition
lrs0 = LayersReader('D:/tmp/girs/DEU_adm_shp/DEU_adm4.shp')
lrs1 = lrs0.transform('D:/tmp/girs/DEU_adm_shp/DEU_adm4_copy.shp', epsg=32632)
fields_to_delete = lrs1.get_field_names()
fields_to_delete.remove('NAME_4')
lrs1.delete_fields(fields_to_delete)
lrs1.show(max_rows=4)

fields = [FieldDefinition(name='AREA', oft_type=ogr.OFTReal, width=18, precision=6)]
lrs1.add_fields(fields=fields)
lrs1.show(max_rows=4)

lrs1.calculate_area('AREA', scale=1.0 / 1000000.0)
lrs1.show(max_rows=4)
        _GEOM_        NAME_4
FID
0      Polygon  Allmendingen
1      Polygon       Altheim
...        ...           ...
11300  Polygon     Wohlsborn
11301  Polygon        Weimar

[11302 rows x 2 columns]
        _GEOM_        NAME_4  AREA
FID
0      Polygon  Allmendingen   NaN
1      Polygon       Altheim   NaN
...        ...           ...   ...
11300  Polygon     Wohlsborn   NaN
11301  Polygon        Weimar   NaN

[11302 rows x 3 columns]
        _GEOM_        NAME_4       AREA
FID
0      Polygon  Allmendingen  46.050899
1      Polygon       Altheim   7.711316
...        ...           ...        ...
11300  Polygon     Wohlsborn   4.077278
11301  Polygon        Weimar  84.403305

[11302 rows x 3 columns]

Drivers

Drivers open a dataset to read, update, and create data. The driver short name can be explicitly given as parameter in functions or retrieved from file name extensions. The latter applies only for unambiguous file name extensions (suffixes).

Unambiguous suffix:

      Suffix         Drivers
0        000             S57
1        bna             BNA
2        csv             CSV
3        dat          XPlane
4        dgn             DGN
5        dxf             DXF
6        e00          AVCE00
7        gdb     OpenFileGDB
8    geojson         GeoJSON
9        gml             GML
10       gmt         OGR_GMT
11       gpx             GPX
12       gtm   GPSTrackMaker
13       gtz   GPSTrackMaker
14       gxt      Geoconcept
15       jml             JML
16      json         GeoJSON
17       kml             KML
18       map            WAsP
19       mid    MapInfo File
20       mif    MapInfo File
21        nc          netCDF
22       pix          PCIDSK
23       rec             REC
24       shp  ESRI Shapefile
25       sql          PGDUMP
26       svg             SVG
27       sxf             SXF
28       tab    MapInfo File
29       thf          EDIGEO
30  topojson         GeoJSON
31       vct          Idrisi
32       vrt         OGR_VRT
33       x10             VDV
34       xml             NAS

Ambiguous suffix:

  Suffix          Drivers
0    mdb   PGeo, Geomedia
1    txt  Geoconcept, VDV

Files without suffix:

DB2ODBC, UK .NTF, OGR_SDTS, Memory, ODBC, MSSQLSpatial, GeoRSS, GPSBabel, SUA, OpenAir, OGR_PDS,
HTF, AeronavFAA, ARCGEN, SEGUKOOA, SEGY, Walk, Selafin, TIGER, AVCBin

Driver are retrieved using FeatDrivers.get_driver(**kwargs). The parameters are any of drivername, extension, or filename in this ranking order. drivername=None is interpreted as not defined. If drivername is undefined, filename='' or filename=None requests a Memory driver.

print 1, FeatDrivers.get_driver().GetName()
print 2, FeatDrivers.get_driver(source='').GetName()
print 3, FeatDrivers.get_driver(source='some_source', drivername='Memory').GetName()
print 4, FeatDrivers.get_driver(source=None).GetName()
print 5, FeatDrivers.get_driver(source='a.gml').GetName()
print 6, FeatDrivers.get_driver(source='a.shp').GetName()
# print FeatDrivers.get_driver(source='a.mdb').GetName()  # ERROR, since ambiguous
print 7, FeatDrivers.get_driver(source='a.mdb', drivername='Geomedia').GetName()  # source='a.mdb' not used
1 Memory
2 Memory
3 Memory
4 Memory
5 GML
6 ESRI Shapefile
7 Geomedia