How do I get the extent of tables (feature classes) in a geopackage?

1555
7
06-03-2020 10:03 AM
EricEagle
Occasional Contributor III
import arcpy
import os

gpkg = r"C:\Data\georgia.gpkg"

desc = arcpy.Describe(gpkg)
print(desc.dataType)
print(desc.workspaceType)

Result:

Workspace
LocalDatabase

walk = arcpy.da.Walk(gpkg)

for path, names, filenames in walk:
    for fname in filenames:
        d = arcpy.Describe(os.path.join(path, fname))
        print(fname, d.dataType)

Result:

main.ga_admin FeatureClass
main.ga_highway FeatureClass
main.ga_natural FeatureClass
main.ga_poi FeatureClass
main.ga_water FeatureClass

But if I add...

for path, names, filenames in walk:
    for fname in filenames:
        d = arcpy.Describe(os.path.join(path, fname))
        print(fname, d.dataType)
        print(f"Extent of feature class: {d.extent.XMin}, {d.extent.YMin}, {d.extent.XMax}, {d.extent.YMax}")
        print(d.extent) # sanity check

Result:

main.ga_admin FeatureClass
Extent of feature class: nan, nan, nan, nan
nan nan nan nan NaN NaN NaN NaN
main.ga_highway FeatureClass
Extent of feature class: nan, nan, nan, nan
nan nan nan nan NaN NaN NaN NaN
main.ga_natural FeatureClass
Extent of feature class: nan, nan, nan, nan
nan nan nan nan NaN NaN NaN NaN
main.ga_poi FeatureClass
Extent of feature class: nan, nan, nan, nan
nan nan nan nan NaN NaN NaN NaN
main.ga_water FeatureClass
Extent of feature class: nan, nan, nan, nan
nan nan nan nan NaN NaN NaN NaN

No extent.  However, if I load this content in ArcGIS Pro, it calculates the extent correctly.

How do I get the extent of layers in a geopackage?

0 Kudos
7 Replies
JoshuaBixby
MVP Esteemed Contributor

I tested your code with a GeoPackage I just created, and it works fine.  So, I am wondering where did this GeoPackage come from?  If third-party software created the GeoPackage, it may be that not all of the GeoPackage data tables were properly populated.  If the GeoPackage was created by third-party software, try copying the contents to a new GPKG you create from Pro, and then see if you get expected results.

0 Kudos
EricEagle
Occasional Contributor III

Thanks Joshua, this was a GPKG created from QGIS.  Workflow-wise, we have both QGIS and ArcGIS Pro and if someone is working in QGIS they're generally using GPKG for their file containers, and if they're in Pro, they're using FGDB.  The thing I'm trying to solve is crawling a large SAN with both GPKG and FGDB, and discovering/visualizing the extents of various holdings.

Either way, helpful pointers - I'll give it a shot and report back.

0 Kudos
EricEagle
Occasional Contributor III

Okay, so this is very odd behavior.  When I load the GPKG directly into pro, I am able to use the IPython console and successfully retrieve the extent.  When I do the same calling arcpy independently of an ArcGIS Pro project, it returns nan. behold:

I also tried using RecalculateFeatureClassExtent() on the table; it executed without error but still returned null for the extent when divorced from an ArcGIS Pro session.

I'm leaning toward "bug" or more likely "incomplete support for spec" on this, as it seems like there is some translation work being done at load time in Pro.

I could create GPKG in Pro, but it kind of defeats the purpose.  The workflow I am scripting out assumes all GPKG are created in QGIS 3.x.  I'd prefer to stay away from having to rely on the ogr/gdal python api since it's .... well, what I'd call "high friction" but seeing as the osgeo libraries are distributed with ArcGIS Pro, that may just be the way I have to go in the meanwhile.

0 Kudos
DanPatterson
MVP Esteemed Contributor

I noticed that you are not using the data access module. In theory, it shouldn't matter, but try

da_desc = arcpy.da.Describe(r"...Your_Featureclass_here")

print(da_desc['extent'])  # ---- da.Describe uses a dictionary

300000 4999999.3145 300029.318 5000022.4949 NaN NaN NaN NaN

I got the same results using arcpy.Describe and arcpy.da.Describe, but you never know


... sort of retired...
EricEagle
Occasional Contributor III

Thanks Dan, yeah, same results.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

In the Python console in Pro, the following code is not describing the GPKG feature class but the Pro feature layer.

fc = "main.ga_highway"
desc = arcpy.Describe(fc)
print(desc.extent)

It may be that Pro is calculating or setting the extent for the layer when it is created and loaded into Pro.  That said, when I tested your stand-alone code against a GPKG I created yesterday, it worked fine.

Is there a publicly available GPKG that shares this behavior?  Or, can you share a stripped-down GPKG that has this behavior?

EricEagle
Occasional Contributor III

Hi Joshua,

For some reason GeoNet does not email me when someone responds, so I'm sorry for the lateness of this reply.  I see what you're saying about console querying the layer not the data directly from the gpkg.

From what I can see, this affects GPKG created in QGIS not ArcGIS Pro.

However, most of the time I encounter a geopackage it will have been created in QGIS because why bother in ArcGIS Pro, except for edge cases where a Pro user needs to share data with a QGIS user?

Because gpkg represents an outlier of content that I'm crawling, I've just put this one on the back burner, and will eventually just implement the ogr python api to pull min/max extents.

0 Kudos