How to convert from PostgreSQL to GeoJSON format? - sql

I have a simple table called"imposm3_restaurant" with columns [ id, name, geometry] I want to convert these data into geoJSON, I am using this function
CREATE VIEW imposm3_restaurants_geojson AS SELECT row_to_json(fc) AS geojson FROM
(SELECT 'FeatureCollection' As type, array_to_json(array_agg(f))
As features FROM
(SELECT
'Feature' As type,
ST_AsGeoJSON((lg.geometry),15,0)::json As geometry,
row_to_json((id, name)) As properties
FROM imposm3_restaurants As lg) As f ) As fc;
and the result is this:
{"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"LineString","coordinates":[[2615020.47191046,5899232.25158985],[2615034.50527113,5899231.67978097],[2615033.86145338,5899215.4513157],[2615032.35921198,5899215.51938806],[2615031.96732292,5899205.64890158],[2615034.97180572,5899205.51275702],[2615034.36531075,5899190.07397728],[2615018.19522163,5899190.71385561],[2615018.77372453,5899205.40384137],[2615020.47191046,5899205.32215463],[2615020.91045298,5899216.48601561],[2615019.83742341,5899216.52685903],[2615020.47191046,5899232.25158985]]},"properties":{"f1":2719,"f2":"Atelierul de Pizza"}},{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[2615018.19522163,5899190.71385561],[2615018.77372453,5899205.40384137],[2615020.47191046,5899205.32215463],[2615020.91045298,5899216.48601561],[2615019.83742341,5899216.52685903],[2615020.47191046,5899232.25158985],[2615034.50527113,5899231.67978097],[2615033.86145338,5899215.4513157],[2615032.35921198,5899215.51938806],[2615031.96732292,5899205.64890158],[2615034.97180572,5899205.51275702],[2615034.36531075,5899190.07397728],[2615018.19522163,5899190.71385561]]]},"properties":{"f1":2720,"f2":"Atelierul de Pizza"}},{"type":"Feature","geometry":{"type":"LineString","coordinates":[[2624875.82864931,5903443.39761349],[2624897.49451598,5903452.78251964],[2624901.44139867,5903443.67003443],[2624879.78486269,5903434.29875908],[2624875.82864931,5903443.39761349]]},"properties":{"f1":2986,"f2":"Pizza Acrobatica"}},{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[2624875.82864931,5903443.39761349],[2624897.49451598,5903452.78251964],[2624901.44139867,5903443.67003443],[2624879.78486269,5903434.29875908],[2624875.82864931,5903443.39761349]]]},"properties":{"f1":2988,"f2":"Pizza Acrobatica"}},{"type":"Feature","geometry":{"type":"LineString","coordinates":[[2622460.22447654,5904586.41424973],[2622479.10046632,5904587.95362911],[2622480.25747212,5904573.81314552],[2622461.39081303,5904572.26014582],[2622460.22447654,5904586.41424973]]},"properties":{"f1":3248,"f2":"Casa Vikingilor"}},{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[2622460.22447654,5904586.41424973],[2622479.10046632,5904587.95362911],[2622480.25747212,5904573.81314552],[2622461.39081303,5904572.26014582],[2622460.22447654,5904586.41424973]]]},"properties":{"f1":3249,"f2":"Casa Vikingilor"}},{"type":"Feature","geometry":{"type":"LineString","coordinates":[[2625201.09657005,5897608.45120294],[2625224.46062264,5897614.30435379],[2625241.33051365,5897576.653689],[2625213.43174478,5897570.82778714],[2625201.09657005,5897608.45120294]]},"properties":{"f1":6152,"f2":"Silva"}},{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[2625201.09657005,5897608.45120294],[2625224.46062264,5897614.30435379],[2625241.33051365,5897576.653689],[2625213.43174478,5897570.82778714],[2625201.09657005,5897608.45120294]]]},"properties":{"f1":6153,"f2":"Silva"}},{"type":"Feature","geometry":{"type":"LineString","coordinates":[[2622825.25980629,5904372.27967993],[2622826.15555271,5904353.45341631],[2622834.51585268,5904353.1673446],[2622854.22227404,5904346.00193242],[2622860.03529512,5904362.26715407],[2622856.61093118,5904374.66361634],[2622825.25980629,5904372.27967993]]},"properties":{"f1":6322,"f2":"Restaurant Sinaia"}},{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[2622825.25980629,5904372.27967993],[2622856.61093118,5904374.66361634],[2622860.03529512,5904362.26715407],[2622854.22227404,5904346.00193242],[2622834.51585268,5904353.1673446],[2622826.15555271,5904353.45341631],[2622825.25980629,5904372.27967993]]]},"properties":{"f1":6323,"f2":"Restaurant Sinaia"}}]}
which does not have a current geometry, do you know what is wrong in function: I am using Postgres 9.3 and PostGIS 2.2

Your output is a valid geojson file but the geometries are projected using the projection EPSG:3857.
You can load the data without problems in the most gis desktop applications, in example Quantum Gis.
Probably geojson.io supports only long/lat coordinates EPSG:4326, also try reprojecting the geometries to long/lat coordinates using the function St_Transform
Change this line:
ST_AsGeoJSON((lg.geometry),15,0)::json As geometry,
in this:
ST_AsGeoJSON(ST_Transform(lg.geometry, 4326),15,0)::json As geometry,

Related

ST_Area does not exist Heroku Postgresql + Postgis

I have a Postgres extended with Postgis version 2.5 database in Heroku.
I want to use the function:
ST_Area( a_polygon )
Specifically I want a generated column in my table:
alter table buildings add building_area float generated always as ( st_area( base_polygon ) ) stored;
Where base_polygon is of type polygon.
However, I am getting this error:
ERROR: function st_area(polygon) does not exist Hint: No function matches the given name and argument types. You might need to add explicit type casts.
Aren't these commands supposed to be available after I run CREATE EXTENSION postgis?
Or, is there something else I have to do?
It seems your polygon column data type is postgre base built in polygon.
ST_Area expects postgis geometry type as a parameter.
As in this example from docs https://postgis.net/docs/ST_Area.html
select ST_Area(geom) sqft,
ST_Area(ST_Transform(geom, 26986)) As sqm
from (
select
'SRID=2249;POLYGON((743238 2967416,743238 2967450,
743265 2967450,743265.625 2967416,743238 2967416))' :: geometry
geom
) subquery;
Check if this example works, it means that ST_Area function exists.
You can add a column with postgis geometry type. https://postgis.net/docs/AddGeometryColumn.html
SELECT AddGeometryColumn ('my_schema','my_spatial_table','geom',4326,'POLYGON',2);
Then convert your polygons into postgis format, by postgis functions.
For example https://postgis.net/docs/ST_MakePolygon.html
SELECT ST_MakePolygon( ST_GeomFromText('LINESTRING(75 29,77 29,77 29, 75 29)'));

convert Postgres geometry format to WKT

I have a Postgres table which stores polygon geometry in its specific format in one of the column, something like this-
0103000020E61000000100000004000000B8627F336B1554405DD602FFA2733A40B8627FA7601554403851F8EBC7723A40B8627FC38F15544036D539E90B733A40B8627F336B1554405DD602FFA2733A40
I know how to convert this single value to WKT using ST_AsText which will give me POLYGON((Lat Long)). But I want to convert whole column into WKT format.
How to achieve this?
Thanks!
Have you tried this?
SELECT ST_AsText(your_geom_column) FROM your_table
In the following examples I'll show you a few ways to serialise your geometries. Here is sample data with two points encoded as 4326 (WGS84):
CREATE TEMPORARY TABLE tmp
(geom GEOMETRY);
INSERT INTO tmp VALUES
('SRID=4326;POINT(1 2)'),
('SRID=4326;POINT(2 4)');
Geometries as WKB (Well-Known Binary, default):
SELECT geom FROM tmp;
geom
----------------------------------------------------
0101000020E6100000000000000000F03F0000000000000040
0101000020E610000000000000000000400000000000001040
Geometries as WKT (Well-Known Text) and EWKT (WKT with an explicit Spatial Reference System):
SELECT ST_AsText(geom), ST_AsEWKT(geom) FROM tmp;
st_astext | st_asewkt
------------+----------------------
POINT(1 2) | SRID=4326;POINT(1 2)
POINT(2 4) | SRID=4326;POINT(2 4)
In case you fancy GeoJSON
SELECT ST_AsGeoJSON(geom) FROM tmp;
st_asgeojson
--------------------------------------
{"type":"Point","coordinates":[1,2]}
{"type":"Point","coordinates":[2,4]}
Or even GML
SELECT ST_AsGML(geom) FROM tmp;
st_asgml
-----------------------------------------------------------------------------------
<gml:Point srsName="EPSG:4326"><gml:coordinates>1,2</gml:coordinates></gml:Point>
<gml:Point srsName="EPSG:4326"><gml:coordinates>2,4</gml:coordinates></gml:Point>
The Google Earth enthusiasts also have their fun! Geometries as KML
SELECT ST_AsKML(geom) FROM tmp;
st_askml
-----------------------------------------------
<Point><coordinates>1,2</coordinates></Point>
<Point><coordinates>2,4</coordinates></Point>
Librarians might also wanna take a look at ST_AsMARC21 (added in 3.3)
SELECT ST_AsMARC21(geom) FROM tmp;
st_asmarc21
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
<record xmlns="http://www.loc.gov/MARC21/slim"><datafield tag="034" ind1="1" ind2=" "><subfield code="a">a</subfield><subfield code="d">E0010000</subfield><subfield code="e">E0010000</subfield><subfield code="f">N0020000</subfield><subfield code="g">N0020000</subfield></dataf
ield></record>
<record xmlns="http://www.loc.gov/MARC21/slim"><datafield tag="034" ind1="1" ind2=" "><subfield code="a">a</subfield><subfield code="d">E0020000</subfield><subfield code="e">E0020000</subfield><subfield code="f">N0040000</subfield><subfield code="g">N0040000</subfield></dataf
ield></record>
(2 rows)
And the list goes on! In the PostGIS documentation there are other fancy ways to serialise geometry.
Demo: db<>fiddle

How to calculate area in SQL using geographic coordinates?

Does anybody know what is the problem with my query. I am trying to calculate area using geographical coordinates, but result seems to be too small to be true. 0.00118 sqm. Can anybody help?
SELECT ST_Area(the_geom) As sqm
FROM (SELECT
ST_GeomFromText('POLYGON
(
(14.604514925547997 121.0968017578125,
14.595212295624522 121.08512878417969,
14.567302046916149 121.124267578125,
14.596541266841905 121.14761352539062,
14.604514925547997 121.0968017578125)
)',4326) ) As foo(the_geom)
How accurate should be the calculation?
A solution is to cast GEOMETRY to GEOGRAPHY, which is acceptably accurate for the most use cases:
SELECT ST_Area(the_geom::GEOGRAPHY ) As sqm
FROM (SELECT
ST_GeomFromText('POLYGON
(
(14.604514925547997 121.0968017578125,
14.595212295624522 121.08512878417969,
14.567302046916149 121.124267578125,
14.596541266841905 121.14761352539062,
14.604514925547997 121.0968017578125)
)',4326) ) As foo(the_geom)
The geography type automatically converts degrees to meters.
Depending on your scenario you could also use directly the geography constructor St_GeographyFromText, which accept a WKT string as argument, very similar to ST_GeomFromText
ST_GeographyFromText('POLYGON((14.604514925547997 121.0968017578125,
14.595212295624522 121.08512878417969,
14.567302046916149 121.124267578125,
14.596541266841905 121.14761352539062,
14.604514925547997 121.0968017578125))'
)

Oracle Spatial transform

We have a table in an Oracle Spatial database containing polygons (lying in Austria) in Lambert projection (SRID=1000047), which we want to transform into WGS84 (SRID=8307). After performing
create table Table2 as (
select ..., SDO_CS.TRANSFORM(geometry, 8307) as geometry, ...
from Table1)
the polygons in Table2 all lie practically on a single point north of Paris. What are we doing wrong?
Edit: As an example, the point [11.26555560027597,53.87928275206266] gets transformed to [2.3069645331040123,49.293822884973984]. I get the same results using SRID=4326 instead of 8307.
The sqlplus query select * from cs_srs where srid=100047 returns
PROJCS["LAMBERT",GEOGCS["GCS_MGI",DATUM["D_MGI",SPHEROID["Bessel_1841",6377397.1‌55,299.1528128]], PRIMEM["Greenwich",0],UNIT["Decimal Degree",0.017453292519943295]],PROJECTION["Lambert Conformal Conic"], PARAMETER["False_Easting",400000],PARAMETER["False_Northing",400000], PARAMETER["Central_Meridian",13.33333333333333],PARAMETER["Standard_Parallel_1",‌​46], PARAMETER["Standard_Parallel_2",49],PARAMETER["Scale_Factor",1], PARAMETER["Latitude_Of_Origin",47.5],UNIT["Meter",1]]
Your source SRID (1000047) is not a standard EPSG code, neither is it any coordinate system that ships with any version of Oracle: it looks like one you defined yourself. Can you show us the definition (select * from cs_srs where srid=1000047) ?
Looking at your input (11.26555560027597,53.87928275206266) - that does not look like any projection. It looks to me like some geodetic coordinates, possibly on a different datum than WGS84.
The coordinate system definition you use is that of the standard SRID 31287:
PROJCS[
"MGI / Austria Lambert",
GEOGCS["MGI",
DATUM["Militar-Geographische Institut",
SPHEROID[
"Bessel 1841",
6377397.155,
299.1528128,
AUTHORITY["EPSG", "7004"]],
TOWGS84[577.326, 90.129, 463.919, 5.137, 1.474, 5.297, 2.4232],
AUTHORITY["EPSG", "6312"]],
PRIMEM["Greenwich", 0.000000, AUTHORITY["EPSG","8901"]],
UNIT["degree (supplier to define representation)", 0.0174532925199433, AUTHORITY["EPSG", "9122"]],
AXIS["Lat", NORTH],
AXIS["Long", EAST],
AUTHORITY["EPSG", "4312"]],
PROJECTION ["Lambert Conformal Conic"],
PARAMETER ["Latitude_Of_Origin", 47.5],
PARAMETER ["Central_Meridian", 13.3333333333333333],
PARAMETER ["Standard_Parallel_1", 49.0],
PARAMETER ["Standard_Parallel_2", 46.0],
PARAMETER ["False_Easting", 400000.0],
PARAMETER ["False_Northing", 400000.0],
UNIT["metre", 1.0, AUTHORITY["EPSG", "9001"]],
AXIS["X", NORTH],
AXIS["Y", EAST],
AUTHORITY["EPSG", "31287"]]
The main difference with yours is that yours is missing the datum shift parameters. The other difference is that the standard parallels are reversed: parallel 1 is 49 and parallel 2 is 46 vs. 46 and 49 in your definition.
Here is what I get when I transform the geometry you posted as example (encoded using 31287):
select sdo_cs.transform (
sdo_geometry(2003,31287,null,sdo_elem_info_array(1, 1003, 1),sdo_ordinate_array(607205.274999979, 528729.87700098, 607052.849999979, 528254.154000983, 607080.702999979, 528224.753000982, 607098.889999979, 528220.193000982, 607113.807999979, 528225.393000979, 607272.533999979, 528720.85100098, 607269.772999979, 528724.96700098, 607205.274999979, 528729.87700098)),
4326
)
from dual;
SDO_GEOMETRY(2003, 4326, NULL, SDO_ELEM_INFO_ARRAY(1, 1003, 1), SDO_ORDINATE_ARRAY(16.1442004, 48.62389, 16.1419009, 48.6196637, 16.1422641, 48.6193904, 16.1425084, 48.6193435, 16.1427132, 48.6193854, 16.1451079, 48.623787, 16.1450725, 48.6238249, 16.1442004, 48.62389))
The result looks fine to me. Can you verify that it is ?
What exact version of Oracle do you run? SRID 31287 exists since 10gR2.
The simplest seems to me to use srid 31287 for your data instead of the custom 100047.

Querying Netezza via SquirrelSQL returns WKT geometry in unknown encoding

I am using SquirrelSQL to write and execute SQL queries on a Netezza database. Using Netezza's spatial capabilities (which are essentially the same as those of PostGIS) I've executed a query and returned a single result that contains a geometry. Here's the query, for reference:
SELECT t.SHAPE
FROM (SELECT * FROM OS_AB_PLUS..E12_ADDRESSBASE WHERE POSTCODE = 'RH1 6NE'
AND PAO_START_NUMBER = '14') as a, OS_TOPO..TOPOGRAPHICAREA as t
WHERE inza..ST_Within(a.shape, t.shape) = TRUE
My issue is that the geometry field, which should contain the polygon coordinates represented as Well-Known Text (WKT), looks instead like this:
g¹ AË Affff¬0AÍÌÌÌî0AÒ 3333Ê AÍÌÌÌî0A» Aë0Afffæ» AffffÒ0A¹ AÒ0A333³¹ A3333¿0AŒ AffffÀ0AÍÌÌLŒ Affff¬0AË A¯0AëQ8Ê A3333í0A3333Ê AÍÌÌÌî0A
I can't seem to find anywhere in SquirrelSQL to specify the encoding of VARCHAR columns, and I've seen the column returned without encoding issues in Aginity (another SQL client). Any suggestions on how to proceed would be much appreciated.
Turns out my issue was not really related to encoding at all. The human-readable version of the geometry in a PostGIS-like database will only be returned when ST_AsText is used in the select statement. So my SQL query becomes:
SELECT inza..ST_AsText(t.SHAPE)
FROM (SELECT * FROM OS_AB_PLUS..E12_ADDRESSBASE WHERE POSTCODE = 'RH1 6NE'
AND PAO_START_NUMBER = '14') as a, OS_TOPO..TOPOGRAPHICAREA as t
WHERE inza..ST_Within(a.shape, t.shape) = TRUE
Which returns. as intended:
POLYGON ((526696.15 148931.9, 526703.94 148932.34, 526703.8 148935.2, 526705.5 148935.3, 526705.4 148937.8, 526695.9 148937.35, 526696.15 148931.9))