Package: pygeopkg
pygeopkgRefer tofudgeofor modernized GeoPackage
capabilities, this package not actively maintainedpygeopkgis a Python compatible library that allows for the creation and
population of (write-to) an OGC GeoPackage database, including creating
features within this resource.Installationpygeopkgis available from thePython Package Index.Python CompatibilityThepygeopkglibrary is compatible with Python 2+ and Python 3+.Usagepygeopkgcan be used to:Create a new empty GeoPackage from scratch.Create new Feature Classes within a GeoPackage.Populate Feature Classes with geometry and attributes.Create An Empty GeoPackagefrompygeopkg.core.geopkgimportGeoPackage# Creates an empty geopackagegpkg=GeoPackage.create(r'c:\temp\test.gpkg')GeoPackages are created withthreedefault Spatial References defined
automatically, a pair of Spatial References to handle undefined cases,
and a WGS 84 entry.The definition of the WGS84 entry is flexible - meaning that theWKT for WGS84can be setup per the users liking. As an example, use with
Esri's ArcGIS means either using theEPSG WKTor theESRI WKT. By
default theESRI WKTis used - However, ifEPSG WKTis desired, you
may provide aflavorparameter to the create method specifying EPSG.# Creates an empty geopackage
gpkg = GeoPackage.create(r'c:\temp\test.gpkg', flavor='EPSG')Create A New Feature ClassTo create a new Feature Class in the empty GeoPackage, you will need
to tell the GeoPackage the Spatial Reference of the Feature Class
and the schema (e.g., fields) to be available in the Feature Class.A Feature Class can be created withZorM(or both) enabled. If
either of these options are enabled, the geometry inserted into the
Feature Classmustinclude a value for the option specified.frompygeopkg.core.geopkgimportGeoPackagefrompygeopkg.core.srsimportSRSfrompygeopkg.core.fieldimportFieldfrompygeopkg.shared.enumerationimportGeometryType,SQLFieldTypesgpkg=GeoPackage.create(r'c:\temp\test.gpkg')srs_wkt=('PROJCS["WGS_1984_UTM_Zone_23N",''GEOGCS["GCS_WGS_1984",''DATUM["D_WGS_1984",''SPHEROID["WGS_1984",6378137.0,298.257223563]],''PRIMEM["Greenwich",0.0],''UNIT["Degree",0.0174532925199433]],''PROJECTION["Transverse_Mercator"],''PARAMETER["False_Easting",500000.0],''PARAMETER["False_Northing",0.0],''PARAMETER["Central_Meridian",-45.0],''PARAMETER["Scale_Factor",0.9996],''PARAMETER["Latitude_Of_Origin",0.0],''UNIT["Meter",1.0]]')srs=SRS('WGS_1984_UTM_Zone_23N','EPSG',32623,srs_wkt)fields=(Field('int_fld',SQLFieldTypes.integer),Field('text_fld',SQLFieldTypes.text),Field('test_fld_size',SQLFieldTypes.text,100),Field('test_bool',SQLFieldTypes.boolean))fc=gpkg.create_feature_class('test',srs,fields=fields,shape_type=GeometryType.point)About Spatial References For GeoPackagesSpatial References in GeoPackages are somewhat loosely defined. You
may provide a Spatial Reference of any definition and from any
authority - be that EPSG, ESRI, or another source. This library follows
this lead and has no restriction on the definitions provided. However,
it should be noted that if you would like Feature Classes to
be readable by major software packages, you should provide a
definition which the software can read appropriately. For example, our testing
has found that ArcMap prefers definitions corresponding to its
own WKT format.A very simple Spatial Reference object is provided with this package
for convenience. It requires the name, authority, Spatial Reference ID,
and Spatial Reference well known text. This object should be used when
creating a Feature Class.Insert Records Into A Feature ClassRecords can be inserted into a Feature Class using theinsert_rowsmethod. This method inserts all the rows with a single sql call to
get the best performance.Geometry fields ongpkgFeature Classes created by this code base will
always be namedSHAPE. Geometry inserted into this field must always
beWKB. To createWKB, use the utility functions from the conversion
subpackage. Currently utility functions exists to handle points, lines
and polygons (includingZandMvarieties).This example shows the creation of a random point Feature Class and
builds upon the code from previous examples. Note that the create Feature Class
portion of the code is omitted...fromrandomimportchoice,randintfromstringimportascii_uppercase,digitsfrompygeopkg.conversion.to_geopkg_geomimport(point_to_gpkg_point,make_gpkg_geom_header)frompygeopkg.shared.constantsimportSHAPEfrompygeopkg.core.geopkgimportGeoPackage# NOTE: Builds from previous examples# and assumes existing GeoPackage and feature class!gpkg=GeoPackage(r'c:\temp\test.gpkg')fc=gpkg.get_feature_class('test')# Field objects can also be usedfield_names=[SHAPE,'int_fld','text_fld']# Generate the geometry header once because it is always the samepoint_geom_hdr=make_gpkg_geom_header(fc.srs.srs_id)# Generate some random points and attributesrows=[]foriinrange(10000):rand_str=''.join(choice(ascii_uppercase+digits)for_inrange(10))rand_int=randint(0,1000)rand_x=randint(300000,600000)rand_y=randint(1,100000)wkb=point_to_gpkg_point(point_geom_hdr,rand_x,rand_y)rows.append((wkb,rand_int,rand_str))fc.insert_rows(field_names,rows)Creating OGC Geometry Well Known BinariesAs mentioned, this library supports the creation of point, line, and
polygon well known binaries. Functions supporting these capabilities
can be found inpygeopkg.conversion.to_geopkg_geom.Examples are provided below and further examples can be found in the
tests. See code documentation for more details as warranted.It is important to note thatZandMcapabilities are defined at the
time a Feature Class is created. If a Feature Class isZorMenabled,
then a value must be provided for that value. Be sure to pick the
correct conversion function depending on theZandMcombination
desired.Point ExampleA binary header with srs details is always needed but (inpygeopkg)
is the same for all features in a Feature Class. For best performance,
create this once.# Point in WGS 84x,y=-119,34hdr=make_gpkg_geom_header(4326)gpkg_wkb=point_to_gpkg_point(hdr,x,y)Line ExampleThe utility function for creating lines expects a list of points
representing its vertices.# Line with ZM Values for use with UTM Zone 23N (WGS 84)line=[(300000,1,10,0),(300000,4000000,20,1000),(700000,4000000,30,2000),(700000,1,40,3000)]hdr=make_gpkg_geom_header(32623)gpkg_wkb=points_zm_to_gpkg_line_string_zm(hdr,line)Polygon ExampleThe utility function for creating regular polygons expects a list of
rings where a ring is simply the list of points it contains.rings=[[(300000,1),(300000,4000000),(700000,4000000),(700000,1),(300000,1)]]hdr=make_gpkg_geom_header(32623)gpkg_wkb=point_lists_to_gpkg_polygon(hdr,rings)LicenseMIT