Skip to content

Instantly share code, notes, and snippets.

@guitar1999
Last active February 27, 2017 09:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guitar1999/9442467 to your computer and use it in GitHub Desktop.
Save guitar1999/9442467 to your computer and use it in GitHub Desktop.
QGIS 2.2 and PostGIS geometry > 2d
# QGIS 2.2 won't show 4d geometry (maybe not 3dm or 3dz either but I didn't test).
# If you don't actually need 4d and it's just an artifact of the data when you loaded
# it, use this python snippet to convert it to 2d and get your data showing up in
# QGIS again. Use at your own risk. There is NO ERROR HANDLING HERE!
import psycopg2
db = psycopg2.connect(host='localhost', port='5432', database='#####',user='#####')
cursor = db.cursor()
# Customize as necessary to keep the tables where you need 4d
query = "SELECT f_table_schema, f_table_name AS table, f_geometry_column, srid, type FROM geometry_columns WHERE coord_dimension > 2 AND NOT f_table_name LIKE '%flowline%';"
cursor.execute(query)
rels = cursor.fetchall()
for schema, rel, geom, srid, gtype in rels:
query = """ALTER TABLE {0}.{1} ALTER COLUMN {2} TYPE geometry({3},{4}) USING ST_Force2d({2});""".format(schema, rel, geom, gtype, srid)
cursor.execute(query)
db.commit()
cursor.close()
db.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment