2

Is there an option in PostGIS (in query or table option) to set the return type of a Geometry?

I'm using this query:

select * from "locations" where "locations"."id" = 2 limit 1

And that will return

2;"My Home";"My Address";"0101000020E6100000EC14AB0661855EC010CF126404B64240"

But I want to default return the geometry as JSON, without naming the parameters, I mean, I don't want to do:

select id, name, address, ST_AsGeoJSON (location) from "locations" where "locations"."id" = 2 limit 1

I just want to continue using select * and returning location as GeoJSON by default. Is this possible?

3
  • 1
    Create a view and select * it.
    – WKT
    Commented Jun 25, 2016 at 17:48
  • @wkt Could you please elaborate is as an answer? I have 0 experience with PostGIS Commented Jun 25, 2016 at 17:51
  • I think your title is a ambiguous. When I read "type of geometry", my thought goes to "Point", "Linestring" etc. etc. I'd suggest the term "geometry representation" or "geometry format" instead.
    – raphael
    Commented Jun 26, 2016 at 16:22

1 Answer 1

3
  1. Create a view

    CREATE VIEW v_location AS SELECT id, name, address, ST_AsGeoJSON (location) AS location FROM "locations";

  2. Use the "SELECT *" on it:

    SELECT * FROM v_location where "locations"."id" = 2 limit 1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.