Search code examples
postgresqlgeometrygispostgis

How to make a PostGIS LINESTRING with only one initial point


I have a service that will be sending GPS coordinates as it moves, and these will be added to my PostgreSQL / PostGIS database.

Right now, created a table with one column that is a Geometry("LINESTRING") type. However, it seems that in order to make a LINESTRING, you need at least two points.

But, I will only be receiving one point at a time. So initially when the service starts sending me location data, it only has one location to send. I will have to wait for the second location submission in order to have at least two points. So what should I do when I receive the first point since I can't make a LINESTRING out of it yet?

What is the recommended procedure to follow in this case for the initial case when I only have one point so far?


Solution

  • Using ST_MakeLine to create LineStrings you will be able to temporary add your one-point-line into a LineString geometry column:

    Quick&Dirty:

    CREATE TEMPORARY TABLE t (id int, geom Geometry(LINESTRING));  
    
    INSERT INTO t VALUES (1, (SELECT ST_MakeLine('POINT(1 2)'::geometry)));
    SELECT id,ST_AsText(geom) FROM t;
    
     id |    st_astext    
    ----+-----------------
      1 | LINESTRING(1 2)
    (1 Zeile)
    

    Then updating it with the same function should also work:

    UPDATE t SET geom = ST_MakeLine(geom,'POINT(2 3)')
    WHERE id = 1;
    
    SELECT id,ST_AsText(geom) FROM t;
    
     id |      st_astext      
    ----+---------------------
      1 | LINESTRING(1 2,2 3)
    (1 Zeile)