Search code examples
geospatialsinglestore

Automatically compute GEOGRAPHY polygon from inserted values in MemSQL


In MemSQL I can compute a GEOGRAPHYPOINT automatically from inserted values easily:

CREATE TABLE point_test (
  id INTEGER NOT NULL,
  lon DECIMAL,
  lat DECIMAL,
  point AS GEOGRAPHY_POINT(lon, lat) PERSISTED GEOGRAPHYPOINT,
  PRIMARY KEY (id)
);

How would I do the same with a GEOGRAPHY polygon? I've tried several variations like

CREATE TABLE area_test (
  id INTEGER NOT NULL,
  lon1 DECIMAL,
  lon2 DECIMAL,
  lat1 DECIMAL,
  lat2 DECIMAL,
  area AS GEOGRAPHY_AREA("POLYGON((lon1 lat1, lon1 lat2, lon2 lat2, lon2 lat1, lon1 lat1))") PERSISTED GEOGRAPHY,
  PRIMARY KEY (id)
);

but to no avail. I can see that the fields in the polygon are not expanded during insert, hence leading to a NULL area, but I can't seem to create a valid expression for GEOGRAPHY_AREA in the same manner as for GEOGRAPHY_POINT.

EDIT: I was able to solve the issue with the hints provided by Joseph Victor. A GEOGRAPHY can effectively be computed on insert as follows:

CREATE TABLE area_test (
 id INTEGER NOT NULL,
 lon1 DECIMAL,
 lon2 DECIMAL,
 lat1 DECIMAL,
 lat2 DECIMAL,
 area AS CONCAT("POLYGON((", lon1," ",lat1,",",lon1," ",lat2,",",lon2," ",lat2,",",lon2," ",lat1,",",lon1," ",lat1,"))") PERSISTED GEOGRAPHY,
 PRIMARY KEY (id)
);

Solution

  • So, this kind of sucks, but the lat1 things are in a string. Also, GEOAGRAPHY_AREA returns a double, not a GEOGRAPHY. Try

    area AS GEOGRAPHY_AREA(CONCAT("POLYGON((",
                                  lon1,
                                  " ",
                                  lon2,
                                  ",",
     ...
                                  ")")) PERSISTED DOUBLE