Search code examples
pythonsqlpostgresqlpostgis

What is the suitable value to initialize an empty column of type geometry


i want to set a default value to a geometry column indicates that it is null or empty.for example, a string initially is initialized to null or "" to indicate being void/empty. what is the equivalent to null/empty for geometries. what is the value to be set to a geometry column inidicates that it is empty. i tried 0 and "" but they are not allowed to be inserted in a column of type geometry


Solution

  • Add a DEFAULT to the geometry column with an empty geometry, e.g. for POINT:

    CREATE TABLE t (
      id int,
      geom geometry(point,4326) NOT NULL DEFAULT 'POINT EMPTY'
    );
    

    It applies also to other geometry types (and their Z and M extensions as well), e.g.:

    SELECT 
      'POINT EMPTY'::geometry,
      'POLYGON EMPTY'::geometry,
      'LINESTRING EMPTY'::geometry,
      'MULTILINESTRING EMPTY'::geometry,
      'MULTIPOLYGON EMPTY'::geometry,
      'MULTIPOINT EMPTY'::geometry,
      'POLYHEDRALSURFACE EMPTY'::geometry,
      'TRIANGLE EMPTY'::geometry,
      'TIN EMPTY'::geometry,
      'GEOMETRYCOLLECTION EMPTY'::geometry;
    
    -[ RECORD 1 ]----------------------------------------
    geometry | 0101000000000000000000F87F000000000000F87F
    geometry | 010300000000000000
    geometry | 010200000000000000
    geometry | 010500000000000000
    geometry | 010600000000000000
    geometry | 010400000000000000
    geometry | 010F00000000000000
    geometry | 011100000000000000
    geometry | 011000000000000000
    geometry | 010700000000000000
    

    Demo: db<>fiddle