pygeopkg


Namepygeopkg JSON
Version 0.1.3 PyPI version JSON
download
home_pagehttps://github.com/realiii/pygeopkg
SummaryA Python library that allows for the creation and population of OGC GeoPackage databases with write access.
upload_time2023-07-04 16:56:19
maintainer
docs_urlNone
authorIntegrated Informatics Inc.
requires_python
licenseCopyright 2019, Integrated Informatics Inc. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords geopackage
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # pygeopkg

*Refer to [fudgeo](https://pypi.org/project/fudgeo/) for modernized GeoPackage
capabilities, this package not actively maintained*

**pygeopkg** is a Python compatible library that allows for the creation and
population of (*write-to*) an OGC GeoPackage database, including creating 
features within this resource. 


## Installation

**pygeopkg** is available from the [Python Package Index](https://pypi.org/project/pygeopkg/).


### Python Compatibility

The **pygeopkg** library is compatible with Python 2+ and Python 3+.


## Usage

**pygeopkg** can 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 GeoPackage

```python
from pygeopkg.core.geopkg import GeoPackage

# Creates an empty geopackage
gpkg = GeoPackage.create(r'c:\temp\test.gpkg')
```

GeoPackages are created with *three* default 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 the 
*WKT for WGS84* can be setup per the users liking. As an example, use with 
Esri's ArcGIS means either using the *EPSG WKT* or the *ESRI WKT*. By
default the *ESRI WKT* is used - However, if *EPSG WKT* is desired, you
may provide a ``flavor`` parameter to the create method specifying EPSG.

```
# Creates an empty geopackage
gpkg = GeoPackage.create(r'c:\temp\test.gpkg', flavor='EPSG')
```


### Create A New Feature Class

To 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 with *Z* or *M* (or both) enabled. If 
either of these options are enabled, the geometry inserted into the 
Feature Class **must** include a value for the option specified.

```python
from pygeopkg.core.geopkg import GeoPackage
from pygeopkg.core.srs import SRS
from pygeopkg.core.field import Field
from pygeopkg.shared.enumeration import GeometryType, SQLFieldTypes

gpkg = 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 GeoPackages

Spatial 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 Class

Records can be inserted into a Feature Class using the ``insert_rows`` 
method. This method inserts all the rows with a single sql call to 
get the best performance.

Geometry fields on **gpkg** Feature Classes created by this code base will
always be named ``SHAPE``. Geometry inserted into this field must always
be *WKB*. To create *WKB*, use the utility functions from the conversion 
subpackage. Currently utility functions exists to handle points, lines
and polygons (including *Z* and *M* varieties).

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...

```python
from random import choice, randint
from string import ascii_uppercase, digits
from pygeopkg.conversion.to_geopkg_geom import (
    point_to_gpkg_point, make_gpkg_geom_header)
from pygeopkg.shared.constants import SHAPE
from pygeopkg.core.geopkg import GeoPackage

# 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 used
field_names = [SHAPE, 'int_fld', 'text_fld']

# Generate the geometry header once because it is always the same
point_geom_hdr = make_gpkg_geom_header(fc.srs.srs_id)

# Generate some random points and attributes
rows = []
for i in range(10000):
    rand_str = ''.join(choice(ascii_uppercase + digits) for _ in range(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 Binaries

As mentioned, this library supports the creation of point, line, and 
polygon well known binaries. Functions supporting these capabilities 
can be found in ``pygeopkg.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 that *Z* and *M* capabilities are defined at the
time a Feature Class is created. If a Feature Class is *Z* or *M* enabled,
then a value must be provided for that value. Be sure to pick the 
correct conversion function depending on the *Z* and *M* combination 
desired.


#### Point Example

A binary header with srs details is always needed but (in **pygeopkg**) 
is the same for all features in a Feature Class. For best performance,
create this once. 

```python
# Point in WGS 84
x, y = -119, 34
hdr = make_gpkg_geom_header(4326)
gpkg_wkb = point_to_gpkg_point(hdr, x, y)
```


#### Line Example

The utility function for creating lines expects a list of points 
representing its vertices.

```python
# 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 Example

The utility function for creating regular polygons expects a list of 
rings where a ring is simply the list of points it contains.

```python
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)
```


## License

[MIT](https://choosealicense.com/licenses/mit/)


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/realiii/pygeopkg",
    "name": "pygeopkg",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "geopackage",
    "author": "Integrated Informatics Inc.",
    "author_email": "\"Integrated Informatics Inc.\" <contact@integrated-informatics.com>",
    "download_url": "",
    "platform": null,
    "description": "# pygeopkg\n\n*Refer to [fudgeo](https://pypi.org/project/fudgeo/) for modernized GeoPackage\ncapabilities, this package not actively maintained*\n\n**pygeopkg** is a Python compatible library that allows for the creation and\npopulation of (*write-to*) an OGC GeoPackage database, including creating \nfeatures within this resource. \n\n\n## Installation\n\n**pygeopkg** is available from the [Python Package Index](https://pypi.org/project/pygeopkg/).\n\n\n### Python Compatibility\n\nThe **pygeopkg** library is compatible with Python 2+ and Python 3+.\n\n\n## Usage\n\n**pygeopkg** can be used to: \n* Create a new empty GeoPackage from scratch.\n* Create new Feature Classes within a GeoPackage.\n* Populate Feature Classes with geometry and attributes. \n\n\n### Create An Empty GeoPackage\n\n```python\nfrom pygeopkg.core.geopkg import GeoPackage\n\n# Creates an empty geopackage\ngpkg = GeoPackage.create(r'c:\\temp\\test.gpkg')\n```\n\nGeoPackages are created with *three* default Spatial References defined\nautomatically, a pair of Spatial References to handle undefined cases,\nand a WGS 84 entry. \n\nThe definition of the WGS84 entry is flexible - meaning that the \n*WKT for WGS84* can be setup per the users liking. As an example, use with \nEsri's ArcGIS means either using the *EPSG WKT* or the *ESRI WKT*. By\ndefault the *ESRI WKT* is used - However, if *EPSG WKT* is desired, you\nmay provide a ``flavor`` parameter to the create method specifying EPSG.\n\n```\n# Creates an empty geopackage\ngpkg = GeoPackage.create(r'c:\\temp\\test.gpkg', flavor='EPSG')\n```\n\n\n### Create A New Feature Class\n\nTo create a new Feature Class in the empty GeoPackage, you will need\nto tell the GeoPackage the Spatial Reference of the Feature Class\nand the schema (e.g., fields) to be available in the Feature Class.\n\nA Feature Class can be created with *Z* or *M* (or both) enabled. If \neither of these options are enabled, the geometry inserted into the \nFeature Class **must** include a value for the option specified.\n\n```python\nfrom pygeopkg.core.geopkg import GeoPackage\nfrom pygeopkg.core.srs import SRS\nfrom pygeopkg.core.field import Field\nfrom pygeopkg.shared.enumeration import GeometryType, SQLFieldTypes\n\ngpkg = GeoPackage.create(r'c:\\temp\\test.gpkg')\n\nsrs_wkt = (\n    'PROJCS[\"WGS_1984_UTM_Zone_23N\",'\n    'GEOGCS[\"GCS_WGS_1984\",'\n    'DATUM[\"D_WGS_1984\",'\n    'SPHEROID[\"WGS_1984\",6378137.0,298.257223563]],'\n    'PRIMEM[\"Greenwich\",0.0],'\n    'UNIT[\"Degree\",0.0174532925199433]],'\n    'PROJECTION[\"Transverse_Mercator\"],'\n    'PARAMETER[\"False_Easting\",500000.0],'\n    'PARAMETER[\"False_Northing\",0.0],'\n    'PARAMETER[\"Central_Meridian\",-45.0],'\n    'PARAMETER[\"Scale_Factor\",0.9996],'\n    'PARAMETER[\"Latitude_Of_Origin\",0.0],'\n    'UNIT[\"Meter\",1.0]]')\n\nsrs = SRS('WGS_1984_UTM_Zone_23N', 'EPSG', 32623, srs_wkt)\nfields = (\n    Field('int_fld', SQLFieldTypes.integer),\n    Field('text_fld', SQLFieldTypes.text),\n    Field('test_fld_size', SQLFieldTypes.text, 100),\n    Field('test_bool', SQLFieldTypes.boolean))\n\nfc = gpkg.create_feature_class(\n    'test', srs, fields=fields, shape_type=GeometryType.point)\n```\n\n\n#### About Spatial References For GeoPackages\n\nSpatial References in GeoPackages are somewhat loosely defined. You\nmay provide a Spatial Reference of any definition and from any\nauthority - be that EPSG, ESRI, or another source. This library follows\nthis lead and has no restriction on the definitions provided. However,\nit should be noted that if you would like Feature Classes to\nbe readable by major software packages, you should provide a\ndefinition which the software can read appropriately. For example, our testing\nhas found that ArcMap prefers definitions corresponding to its\nown WKT format.\n\nA very simple Spatial Reference object is provided with this package\nfor convenience. It requires the name, authority, Spatial Reference ID,\nand Spatial Reference well known text. This object should be used when\ncreating a Feature Class.\n\n\n### Insert Records Into A Feature Class\n\nRecords can be inserted into a Feature Class using the ``insert_rows`` \nmethod. This method inserts all the rows with a single sql call to \nget the best performance.\n\nGeometry fields on **gpkg** Feature Classes created by this code base will\nalways be named ``SHAPE``. Geometry inserted into this field must always\nbe *WKB*. To create *WKB*, use the utility functions from the conversion \nsubpackage. Currently utility functions exists to handle points, lines\nand polygons (including *Z* and *M* varieties).\n\nThis example shows the creation of a random point Feature Class and\nbuilds upon the code from previous examples. Note that the create Feature Class\nportion of the code is omitted...\n\n```python\nfrom random import choice, randint\nfrom string import ascii_uppercase, digits\nfrom pygeopkg.conversion.to_geopkg_geom import (\n    point_to_gpkg_point, make_gpkg_geom_header)\nfrom pygeopkg.shared.constants import SHAPE\nfrom pygeopkg.core.geopkg import GeoPackage\n\n# NOTE: Builds from previous examples \n# and assumes existing GeoPackage and feature class!\n\ngpkg = GeoPackage(r'c:\\temp\\test.gpkg')\nfc = gpkg.get_feature_class('test')\n\n# Field objects can also be used\nfield_names = [SHAPE, 'int_fld', 'text_fld']\n\n# Generate the geometry header once because it is always the same\npoint_geom_hdr = make_gpkg_geom_header(fc.srs.srs_id)\n\n# Generate some random points and attributes\nrows = []\nfor i in range(10000):\n    rand_str = ''.join(choice(ascii_uppercase + digits) for _ in range(10))\n    rand_int = randint(0, 1000)\n    rand_x = randint(300000, 600000)\n    rand_y = randint(1, 100000)\n    wkb = point_to_gpkg_point(point_geom_hdr, rand_x, rand_y)\n    rows.append((wkb, rand_int, rand_str))\n\nfc.insert_rows(field_names, rows)\n```\n\n\n### Creating OGC Geometry Well Known Binaries\n\nAs mentioned, this library supports the creation of point, line, and \npolygon well known binaries. Functions supporting these capabilities \ncan be found in ``pygeopkg.conversion.to_geopkg_geom``. \n\nExamples are provided below and further examples can be found in the \ntests. See code documentation for more details as warranted.\n\nIt is important to note that *Z* and *M* capabilities are defined at the\ntime a Feature Class is created. If a Feature Class is *Z* or *M* enabled,\nthen a value must be provided for that value. Be sure to pick the \ncorrect conversion function depending on the *Z* and *M* combination \ndesired.\n\n\n#### Point Example\n\nA binary header with srs details is always needed but (in **pygeopkg**) \nis the same for all features in a Feature Class. For best performance,\ncreate this once. \n\n```python\n# Point in WGS 84\nx, y = -119, 34\nhdr = make_gpkg_geom_header(4326)\ngpkg_wkb = point_to_gpkg_point(hdr, x, y)\n```\n\n\n#### Line Example\n\nThe utility function for creating lines expects a list of points \nrepresenting its vertices.\n\n```python\n# Line with ZM Values for use with UTM Zone 23N (WGS 84)\nline = [(300000, 1, 10, 0), (300000, 4000000, 20, 1000),\n        (700000, 4000000, 30, 2000), (700000, 1, 40, 3000)]\nhdr = make_gpkg_geom_header(32623)\ngpkg_wkb = points_zm_to_gpkg_line_string_zm(hdr, line)\n```\n\n\n#### Polygon Example\n\nThe utility function for creating regular polygons expects a list of \nrings where a ring is simply the list of points it contains.\n\n```python\nrings = [[(300000, 1), (300000, 4000000), (700000, 4000000),\n          (700000, 1), (300000, 1)]]\nhdr = make_gpkg_geom_header(32623)\ngpkg_wkb = point_lists_to_gpkg_polygon(hdr, rings)\n```\n\n\n## License\n\n[MIT](https://choosealicense.com/licenses/mit/)\n\n",
    "bugtrack_url": null,
    "license": "Copyright 2019, Integrated Informatics Inc.  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.  THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.",
    "summary": "A Python library that allows for the creation and population of OGC GeoPackage databases with write access.",
    "version": "0.1.3",
    "project_urls": {
        "Homepage": "https://github.com/realiii/pygeopkg"
    },
    "split_keywords": [
        "geopackage"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "eb24f2993695fbde9fc876c2b479c7fc5a642728390304cd1237cfd1ebb5f508",
                "md5": "f0260b656b803b7c7e1d30ce7b260981",
                "sha256": "b408bc913de94addfb460fc64c0cce9929691c28748d9802b2153214408ec029"
            },
            "downloads": -1,
            "filename": "pygeopkg-0.1.3-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "f0260b656b803b7c7e1d30ce7b260981",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 20355,
            "upload_time": "2023-07-04T16:56:19",
            "upload_time_iso_8601": "2023-07-04T16:56:19.506733Z",
            "url": "https://files.pythonhosted.org/packages/eb/24/f2993695fbde9fc876c2b479c7fc5a642728390304cd1237cfd1ebb5f508/pygeopkg-0.1.3-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-07-04 16:56:19",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "realiii",
    "github_project": "pygeopkg",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "pygeopkg"
}
        
Elapsed time: 0.08578s