I have create a Python script where use PostGIS queries to automate some intersection tasks using PostGIS database.
In my database I have polygons, points and lines.
Here's a snippet of my script :
try:
if str(geomtype) == 'point':
geomtype = 1
elif str(geomtype) == 'linestring':
geomtype = 2
elif str(geomtype) == 'polygon':
geomtype = 3
else:
raise TypeError()
sql = "\nDROP TABLE IF EXISTS {0}.{1};\nCREATE TABLE {0}.{1} AS (SELECT {4},\n(st_dump(ST_CollectionExtract(st_intersection(dbgis.{2}.shape,dbgis.{3}.shape)::GEOMETRY(GEOMETRY,2345),{5}))).geom AS shape\nFROM dbgis.{2}, dbgis.{3} WHERE st_intersects(dbgis.{2}.shape,dbgis.{3}.shape) AND {5}=3);\nCREATE INDEX idx_{2}_{3}_{6} ON {0}.{1} USING GIST (shape);\nALTER TABLE {0}.{1} ADD COLUMN id SERIAL;\nALTER TABLE {0}.{1} ADD COLUMN my_area double precision;\nUPDATE {0}.{1} SET my_area = ST_AREA(shape::GEOMETRY);\nALTER TABLE {0}.{1} ADD PRIMARY KEY (id);\nSELECT pop_g_col('{0}.{1}'::REGCLASS);\nGRANT ALL ON {0}.{1} TO GROUP u_cl;\nGRANT ALL ON {0}.{1} TO GROUP my_user;\n-- VACUUM ANALYZE {0}.{1};\n".format(schema, table, tablea, tableb, selects, geomtype, id_gen())
return sql
This PostGIS SQL query works nice, but I need the new field where I want to add my_area
to create only for polygons layers.
That code create for all layers (lines, points, polygons) that field my_area
if layer is point or line then take value 0 I don't like that I don't need it.
How to change this code to create my_area
only in polygons ?