6

I have a problem with integer types when importing a shapefile created in OGR into PostGIS.

I started off with a field created as ogr.OFTInteger64 in OGR/pyqgis as follows:

field_defn = ogr.FieldDefn( fld, ogr.OFTInteger64 ) 
lyr.CreateField ( field_defn )

The field type shows as type: qlonglong, and type name: Integer64 in QGIS.

But after importing the shapefile into PostGIS with shp2pgsql,

shp2pgsql -Id -s 4326 my_lyr.shp my_lyr | psql

the field ended up being a double precision field in \d+, and I had to convert values in this field back to BIGINT before use.

According to PostgreSQL documentation, both bigint and double precision are types 8 byte (64 bit). How can I avoid the confusion and get the intended BIGINT field in PostGIS?

asked Sep 5, 2017 at 20:30
4
  • What happens if you use ogr2ogr for the conversion? Commented Sep 5, 2017 at 20:36
  • 1
    I guess if worst comes to worst, you can pipe the output from shp2pgsql through sed to fix the column definitions... Commented Sep 5, 2017 at 20:57
  • Can you provide a test shapefile? Commented Sep 5, 2017 at 21:19
  • What version of gdal are you using? Commented Sep 6, 2017 at 2:34

1 Answer 1

5

A quick look into this, CreateField is a wrapper around C++'s Layer_CreateField. You can see that bFID64 is supported in the pgdatasource. Perhaps you should check out ogr2ogr -f "PostgreSQL" instead of using shp2pgsql.

PostGIS provides shp2pgsql (not GDAL): it seems to not support 64 bit ints yet, and instead defaults to FTDouble. You can see all the types supported here or here. You can see the function that sets the types here.

Update, I opened a bug report

These two code bases seem to have a lot of overlap. Because gdal is used by PostGIS, it seems that PostGIS should work to get their changes to the ogr data source merged upstream and maintained there.

answered Sep 6, 2017 at 2:55
1

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.