Skip to main content

Hello,

I’m using a Python Caller in an FME workspace. In the python script I’m trying to read in a gpkg file using the code below:
 

#
import geopandas as gpd
import pandas as pd
import os

def FeatureProcessor(feature):
    
    path_gpkg = feature.getAttribute('path_gpkg')
    print(path_gpkg)
    print("file exists - ", os.path.exists(path_gpkg))
    gdf = gpd.read_file(path_gpkg)



However, I get an error saying:

Python Exception <DataSourceError>: '/xxx/abc.gpkg' does not exist in the file system, and is not recognized as a supported dataset name.

It is strange because the line ‘print("file exists - ", os.path.exists(path_gpkg))’ returns True.

Has anyone come across anything similar?

Thanks :)

@hannah-jk you could try to read the gpkg with a standard FeatureReader to check whether that works?
If so then the PythonCaller should be tuned to read it as well 🙂


How are you setting the attribute path_gpkg?

If you hardcode the path in the python does it make any difference?

e.g. path_gpkg = r"C:/Temp/gpkgtest.gpkg"

Have you tried with backslashes instead of forward slashes?


@hannah-jk you could try to read the gpkg with a standard FeatureReader to check whether that works?
If so then the PythonCaller should be tuned to read it as well 🙂

Thanks for the suggestion - using a feature reader does work but reading in the file via a python caller doesn’t work.


How are you setting the attribute path_gpkg?

If you hardcode the path in the python does it make any difference?

e.g. path_gpkg = r"C:/Temp/gpkgtest.gpkg"

Have you tried with backslashes instead of forward slashes?

Thanks - I’ve tried both those suggestions but they don’t work


@hannah-jk for these kind of things I often use Chat GPT. GPT produced this script:

---------------------------------------------------------------------------------------------------------

import os
import geopandas as gpd

def processFeature(feature):
    # Get the input attribute with the path to the GPKG
    path_gpkg = feature.getAttribute('path_gpkg')

    if not path_gpkg:
        feature.setAttribute('gpkg_status', 'No path provided')
        return feature

    if not os.path.exists(path_gpkg):
        feature.setAttribute('gpkg_status', f'File not found: {path_gpkg}')
        return feature

    try:
        # Read the geopackage with GeoPandas
        gdf = gpd.read_file(path_gpkg)

        # Set some useful info back into FME attributes
        feature.setAttribute('gpkg_status', 'Loaded successfully')
        feature.setAttribute('gpkg_feature_count', len(gdf))
        feature.setAttribute('gpkg_crs', str(gdf.crs))

    except Exception as e:
        feature.setAttribute('gpkg_status', f'Failed to read: {e}')

    return feature
------------------------------------------------------------------------------

Be aware:

  1. By default, FME’s bundled Python won’t have GeoPandas installed.
  2. You’ll need to configure FME to use an external Python environment where geopandas, fiona, and shapely are installed, this can be achieved using a pip command in your CMD window.
  3. Make sure the variable 'path_gpkg' is available as an attribute in your workspace pointing to your gpkg. You could also replace this with the hardcoded file location or use File and Pathname Reader.