Postgis longitude, latitude to geometry postgresql - postgresql

I'm migrating all information in a PostGIS system, with full information about thousands of sport spaces. For all this spaces I got latitude and longitude values, and PostGis need a geometry value for that, this column is already in my table "Location".
The SRID of that application is 23030.
I've looked for how to calculate a geometry value from longitude and latitude, and I find that:
update location set point=ST_GeomFromText('POINT('|| longitud || ' ' || latitud ||')',23030);
UPDATE georrepositorio.geometria SET point = ST_SetSRID(ST_Point( longitud, latitud),23030);
UPDATE georrepositorio.geometria SET POINT = ST_SetSRID(ST_MakePoint(longitud,latitud),23030);
I always get a string like: "0101000020F6590000894327550B97114104EA99EA599D4E41"
In the web application which I am building, if I mark the point to locate the space, it insert in table "location" a string like: "0101000020F659000000000020DFB115C00000008053244240" which looks similar to the string I got using those functions.
The problem is that i can't locate each space because there are so many, so I need a massive migration, and using those function to calculate geometry columns doesn't work. Because ok, those functions calculate a geom value, but when you query the application doesn´t show them.
Anyone knows how to calculate geomtry from latitude and longitude, please? anything

First, the geometries are stored as binary, which you see as "0101000020...", which is called well-known binary (WKB). For POINT geometries, you can extract the coordinate ordinals using ST_X(point) for longitude and ST_Y(point) for latitude. You can also use ST_AsText(point) for a human-readable WKT representation.
EPSG:23030 is a projected spatial reference system (SRS), with eastings and northings with units of metres. But if your coordinate data is degrees of latitude and longitude, you need to use a different SRS, such as EPSG:4326. Once stored correctly, the data can be re-projected for the application using ST_Transform(point, 23030).

Related

QGIS not rendering PostGis Geometry column correctly

I'm currently storing a lot of data on a Azure SQL for Postgres instance. My table consists of columns like lat, lon, and geometry which is created using PostGis function
ST_SetSRID(ST_MakePoint(lat,lon),4326))
However, when I import the table into QGIS using a filter on an identifier column and between two timestamps the QGIS Query Builder clearly shows a decent number of records being returned. However, QGIS (I've tried Google Map, OpenStreetMap) renders the Geometry points near Africa (see screenshot below). Looking at some lat and lon pairs on maps.google.com that make up the geometry points, the location is completely different from what QGIS shows.
I've changed the CRS of the project, the Layers for the Map and the Coordinates (i.e. geometry points) to 4326. But still all the data points show up near Africa.
Any ideas what may be causing this and how to resolve it?
PostGIS convention is to pass longitude first (as X coordinate) and latitude second (as Y). So try
ST_MakePoint(lon, lat, 4326)

Lat/Long spatial reference

I am new to PostGIS, am not getting the area of polygon right, my sample data is from Google maps, I know the area of the polygon is 11 acres, but the area returned by st_area doesn't match,
I already referred to a few links like below, but unable to resolve the issue, Internet says google follows 4326 Spatial references, I tried a lot, can you please help, Image attached is the polygon from google maps.
I am expecting an array of such coordinates from the user, I have to calculate the area from PostGIS and give an error back to the user if the area entered is not approximated to calculated area.
https://gis.stackexchange.com/questions/169422/how-does-st-area-in-postgis-work
How do I convert a latitude/longitude pair into a PostGIS geography type?
https://gis.stackexchange.com/questions/56862/what-spatial-reference-system-do-i-store-google-maps-lat-lng-in/56925
17.475197 78.389024
17.4771 78.39044
17.475657 78.391652
17.474408 78.390847
17.475197 78.389024
l_polygon_text='MULTIPOLYGON(((
17.4771000000000001 78.3904399999999981,
17.4751970000000014 78.3890240000000063,
17.4756570000000018 78.3916519999999934,
17.4751970000000014 78.3890240000000063,
17.4744080000000004 78.3908469999999937,
17.4771000000000001 78.3904399999999981)))';
st_area(ST_GeometryFromText(l_polygon_text,4326))
st_area(ST_GeometryFromText(l_polygon_text,2163));
st_area(ST_GeometryFromText(l_polygon_text,2249));
st_area(ST_GeometryFromText(l_polygon_text,3859));
ST_AREA(ST_Transform(ST_GeomFromText(l_polygon_text,4326),31467));
ST_Area(ST_Transform(ST_SetSRID(ST_GeomFromText(l_polygon_text),4326),900913));
polygon
In PostGIS, coordinates must be expressed as longitude first, then latitude. Google uses the opposite.
After swapping the coordinates to the proper order, you can't directly call st_area, else you would get an area in "square degrees" which is meaningless. You would have to project to a suitable local coordinate system, or you can use the geography type which will return an area in m2.
select st_area(st_geogFromText('MULTIPOLYGON(((78.3904399999999981 17.4771000000000001, 78.3890240000000063 17.4751970000000014,78.3916519999999934 17.4756570000000018,78.3890240000000063 17.4751970000000014,78.3908469999999937 17.4744080000000004,78.3904399999999981 17.4771000000000001)))'));
st_area
--------------------
26956.897848576307
That being said, the example you have provided is about 6.5 acres, not 11, because the polygon is not properly defined:

How to store coordinates on mongodb for non latitude/longitude fields?

I'm trying to create 2D space and save coordinates with geojson. I tryied to use 2d index, but when I try to insert and coordinate like 70/50 it tells me that "longitude / latitude is out of bounds".
How can I tell mongodb that this is not the latitude / longitude?
On the official documentation it tells me that "If specifying latitude and longitude coordinates, list the longitude first and then latitude". That "if" makes me believe that I can store something else, not only latitude and logitude, but I do not see how to do it.

How Do I Select Just a Latitude or Longitude From A Geography Point

In Postgresql you can use ST_TEXTFROMGEOGRAPHY in a query to return a geography point in text form. Then whatever reads the query can parse the text to pull out the latitude and longitude of the geography point.
When working with a geometry, you can use ST_X and ST_Y to return the x and y, respectively, of a geometry point as separate columns. I have not found a similar function for geography points.
What is the simplest way to return two columns--a latitude column and a longitude column--from a geography instead of returning the geography as a single column?
I found an answer over gis.stackexchange.com. The answer was in response to a different kind of question, which might be part of why I had a hard time finding it at first.
https://gis.stackexchange.com/questions/32680/postgis-convert-geographypoint-4326-to-gps/32729#32729
The solution is to cast the geography to a geometry first:
SELECT ST_X(geogcolumn::geometry), ST_Y(geogcolumn::geometry) FROM thetable;

How to find if a long/lat point is in the visible bing map

This should be quite simple but I am not getting it.
I have a database of locations with lon/lat specified.
After loading the bing map I get the bounds
var view = map.getBounds();
and then call a webmethod to get all the locations which should be shown (within the bounds of the visible map).
I cannot figure out a query to get the locations (which all have a lon/lat specified) .
This obviously does NOT work as when negative values come into play they mess up the query:
SELECT Location_name, longtitude, latitude FROM location_view WHERE latitude< '40.112' and latitude> '35.783' and longtitude< '28.10453' and longtitude> '19.315'
Is there a normalized way to do this? So the comparison would work?
Your query will work absolutely fine with negative values: a longitude of -130 is still west of a longitude of -120. The only situation in which it won't work is if the bounds of your map crosses the 180th meridian. I.e. the "westmost" longitude is 170 and the "eastmost" latitude is -170.
What database are you using? If you're using SQL Server then you can define each of your locations as a Point using the geography datatype. The geography datatype operates on a round model of the earth, so it will account correctly for crossing the 180th meridian with a query like this:
SELECT Location_name
FROM location_view
WHERE location.STIntersects('POLYGON((19.315 35.783, 28.10453 35.783, 28.10453 40.112, 19.315 40.112, 19.315 35.783))') = 1;