Skip to content

Use Python for custom changes

Use Python when Transform is not enough — for example a custom filter, a field calculation Transform cannot express, or a format-specific cleanup.

Python is optional. Most field renames and column picks should use Transform instead.

You need to be signed in.

How the script is run

GeoJibe runs:

python3 script.py <input_path> <output_path>
  • sys.argv[1] is the input file
  • sys.argv[2] is the output path the script must create

If the output file is missing after the script exits, the step fails with The Python script did not create the output file. The previous data is left unchanged.

The runtime is the same spatial environment GeoJibe uses for Convert. osgeo.gdal and osgeo.ogr are available. pip install is not.

1. Add Python

Add Python from Components.

In Properties, paste a script or use Load .py file. The file must be .py and no larger than 1 MiB.

On Classic this step is Python preprocessing — Optional.

2. Write a script that creates the output

This copy script is the smallest working example. It is the same pattern GeoJibe tests use:

import sys
import shutil

shutil.copy(sys.argv[1], sys.argv[2])

Use this only for single-file formats such as GeoJSON or GeoPackage. For a Shapefile, write a new dataset with OGR instead of copying one file.

3. Rename a field with OGR

This example renames shape_len to shape. It opens the input, writes every feature to the output, and creates sys.argv[2].

import sys
from osgeo import ogr, gdal

gdal.UseExceptions()

src = ogr.Open(sys.argv[1])
if src is None:
    raise SystemExit("Could not open the input file.")

layer = src.GetLayer(0)
drv = ogr.GetDriverByName("GeoJSON")
out = drv.CreateDataSource(sys.argv[2])
if out is None:
    raise SystemExit("Could not create the output file.")

dst = out.CreateLayer(layer.GetName(), layer.GetSpatialRef(), layer.GetGeomType())
defn = layer.GetLayerDefn()
for i in range(defn.GetFieldCount()):
    field = defn.GetFieldDefn(i)
    if field.GetName() == "shape_len":
        renamed = ogr.FieldDefn("shape", field.GetType())
        renamed.SetWidth(field.GetWidth())
        renamed.SetPrecision(field.GetPrecision())
        dst.CreateField(renamed)
    else:
        dst.CreateField(field)

layer.ResetReading()
for feat in layer:
    new_feat = ogr.Feature(dst.GetLayerDefn())
    new_feat.SetGeometry(feat.GetGeometryRef())
    for i in range(defn.GetFieldCount()):
        name = defn.GetFieldDefn(i).GetName()
        dest_name = "shape" if name == "shape_len" else name
        new_feat.SetField(dest_name, feat.GetField(i))
    dst.CreateFeature(new_feat)

src = None
out = None

This matches the OGR pattern used in GeoJibe's Python tests (open argv[1], create argv[2], copy features). The test copy uses GeoJSON; change the driver name if your output path is a GeoPackage (GPKG) or Shapefile (ESRI Shapefile).

Do not call df.rename(...) unless your script actually loads a dataframe and writes sys.argv[2].

4. Test, then Run

On the Canvas, Test / Preview runs the script against the current Source. Then Run the whole workflow.

On Classic, Run & Continue does the same, then you continue to Transform or Convert.

What happens next?

If the script succeeds, GeoJibe inspects the new file and uses it for later steps.

If it fails, you get The Python script failed. and the original data stays in place.

Having trouble?

See My Python script didn't create an output file and the Python scripts reference.

See also