Skip to content
Advertisement

Create Redshift table with new Geometry type through psycopg2

After Redshift announced support for Geometry types and spatial functions, I’d like to create a table with polygons for all countries. I’m failing to do the INSERT and would appreciate help.

Here is what I’ve tried:

I’ve downloaded the geojson and unzipped (https://datahub.io/core/geo-countries)

Then the following python snippet was used to create the table successfully (I’ve used the type GEOMETRY, not sure if I can optimise and use the sub-type POLYGON):

The following script successfully reads the geojson, each entry in “countries” holding a Polygon GeoJson feature:

For those not familiar with GeoJson, it’s simply a set of JSON data that describes geospatial shapes. Here is an excerpt of the data:

Before I insert all countries, I first just want to try and create it for a single country:

The following fails:

With the following error: TypeError: not all arguments converted during string formatting

I’ve also tried

But that gives the following error: psycopg2.errors.InternalError_: Compass I/O exception: Invalid hexadecimal character(s) found

How do I insert a polygon into redshift, using the new Geometry types?

Advertisement

Answer

Here I give the steps that worked to insert it into the DB.

First, a minor correction in creating a table for the geometries, using IDENTITY to have an auto-incrementing ID:

Onto the Geometries. To insert the value, use a WKT value:

Then the following command to insert the value:

Answers from both @Maurice Meyer and @piro guided me to this answer.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement