Visualize PostGIS geography bounding boxes to understand the "&&" operator - gis

I have a point at (-130.2, 30.5) and a box at (-130, 30, -129, 31). As geometry, the && operator reports no bounding box intersection, while as geography, it does:
WITH src(point, envelope) AS (SELECT
ST_SetSRID(ST_MakePoint(-130.2, 30.5), 4326) AS point,
ST_MakeEnvelope(-130.0, 30.0, -129.0, 31.0, 4326) AS envelope)
SELECT
point::GEOMETRY && envelope::GEOMETRY AS geom_bbox_intersects,
point::GEOGRAPHY && envelope::GEOGRAPHY AS geog_bbox_intersects,
ST_Intersects(point::GEOGRAPHY, envelope::GEOGRAPHY) AS geog_poly_intersects
FROM src;
geom_bbox_intersects | geog_bbox_intersects | geog_poly_intersects
----------------------+----------------------+----------------------
f | t | f
Here's what the scenario looks like in 2D (using QGIS in SRID 4326):
I assume for geography, PostGIS is using a rectangular bounding box on the spheroid rather than my longitude-parallel envelope. Is that the case? How might I visualize what's going on in PostGIS using a 2D tool like QGIS?
Versions
PostgreSQL: 10.17
PostGIS: 2.4

Geography use great circle arcs instead of straight lines. To visualize them, you can segmentize a geography into small segments, then convert these segments to geometry
WITH src(geom) AS (SELECT ST_MakeEnvelope(-130.0, 30.0, -129.0, 31.0, 4326) AS geom)
SELECT
st_segmentize(geom::geography,1000)::geometry
FROM src;
That being said, a great circle along a meridian is the meridian itself, so I really fail to see why the geography bounding box are said to overlap. If it fits your workflow, using st_intersects() will return false with the two sample shapes as geography.

Related

QGIS field calculator interpolating over raster

I want to add a z field to my shapefile. Z value is the elevation of the center of the basin. I wonder how should I acomplish that. x and y values of the centroids are in the table. I have the dem of the region.
You'll have to convert those xy coordinates to a point feature class and then you can use the 'Point sampling tool' plugin to assign the value of your basin raster/polygon to each point (similar to the 'Extract Values to Points' tool in ArcGIS).
You can query the raster directly in Field Calculator. Create a new field and populate with:
raster_value('Raster', 1, make_point( x(centroid($geometry)), y(centroid($geometry))))
'Raster' is the raster layer, 1 is the band in the raster layer to use, and the make_point() function generates the centroid.

Selecting polar coordinates from cartesian coordinates in a database

In my MySQL database I have three fields, x,y,z representing a position.
I would like to transform these coordinates into polar coordinates az,el,r, and based on these, select the rows where (e.g.) az are within some region.
How would I go about doing this in MySQL?
EDIT:
This in not a question of how to actually do the coordinate transformation, but rather, if MySQL is capable of transforming the data based on some method, and then selecting data once it is transformed with a criterion based on a comparison of the transformed data.
Solve the Triangle ...
Cartesian = How far along and how far up
Polar = How far away and what angle
In order to convert you need to solve the right triangle for the two known sides
you need to use Pythagoras theorem to find the long side (hypotenuse)
you need the Tangent Function to find the angle
r = √ ( x2 + y2 ) = Pythagoras
θ = tan-1 ( y / x ) = Tangent Function
assuming there's no negative values - then you would have to take the inverse of tan function, or convert them to their positive counterpart
Mysql Pythagorus
SQRT((POWER(242-'EAST',2)) + (POWER(463-'NORT',2))) < 50
assuming your coordinates look like this.... here is an example
http://www.tek-tips.com/viewthread.cfm?qid=1397712
Tangent Function here
http://dev.mysql.com/doc/refman/5.0/en/mathematical-functions.html#function_tan
IMHO this is really a spherical coordinate system maths problem, not a MySQL-specific question.
MySQL just happens to be the data container in this instance.
For any solution you need to work out the maths first, then it becomes a matter of applying the equations to the data.
I can help with MySQL, but I'd have to Google solving these equations and my fingers are tired =)

Converting X,Y to Latitude Longitude to ESRI shapefile

Warning: I know nothing about GIS. That will become very apparent in a moment, of course. My vocabulary isn't going to be spot on, either, Apologies.
I need to recreate parts of a "Strategy Map" that looks like this as "real geo-spatial" map:
Why? Because if I can manage to plot the boxes ("Maximize Shareholder Value", "Exceed Customer Expectations", etc.) on a map in correct relation to each other, I can do some very fun stuff in a data visualization tool I'm working with.
I can build the strategy map above in Visio, and then use a script to export the shapes I care about as X, Y points OR Polygons. One of the boxes above might looks like this once exported:
ShapeNo ShapeName PointNo X Y
1 Exceed Cust 2 37 155
1 Exceed Cust 4 116 155
1 Exceed Cust 6 116 234
1 Exceed Cust 8 37 234
1 Exceed Cust 10 37 155
...or it might look like this:
POLYGON ((37 155, 116 155, 116 134, 37 234, 37,155))
Regardless, I have a bunch of points, and I need to turn these into lat/lon coordinates, using lat/lon (0,0) as my point of reference. In the map above, 0,0 might be beneath the "Exceed Customer Expectations" box - more or less dead center.
Then, I suspect I can find a tool that will convert this jumble of stuff into an ESRI shapefile and I can import directly into my dataviz tool.
Are there any known (free) tools, scripts, libraries, etc.that might do some of this for me?
Your problem shouldn't be solved with a GIS but I can appreciate that you have found some cool dataviz features that require a shapefile.
The problem is that you want to take some x,y points and convert them to lat/lon. Latitude-longitude refer specifically to points on the earth's surface and the points in your problem have no relation to the earth's surface.
Another way to think of this is that you are trying to take random points and say one represents the capital of Russia and the other represents a large city in Germany etc.
Another problem is that you want to have a 0,0 reference point but latitude and longitude have a datum as a reference point which is a specific geographic location.
It's hard to suggest an alternative method to solve your problem without more information on your familiarity with graphic design tools, but lat/lon with GIS are not the direction to be looking.
Many people do convert x,y points to lat/lon but this is not a direct conversion. Cartesian coordinates require a known projection and datum in order for this conversion to be accurate.
Check out this link for an in depth explanation of why arbitrary x,y cannot be converted to lat/lon.
On the other hand, +1 for an out-of-the-box original idea for strategy map design!

Mysql geometry AREA() function returns what exactly when coords are long/lat?

My question is somewhat related to this similar one, which links to a pretty complex solution - but what I want to understand is the result of this:
Using a Mysql Geometry field to store a small polygon I duly ran
select AREA(myPolygon) where id =1
over it, and got an value like 2.345. So can anyone tell me, just what does that number represent seeing as the stored values were long/lat sets describing the polygon?
FYI, the areas I am working on are relatively small (car parks and the like) and the area does not have to be exact - I will not be concerned about the curvature of the earth.
2.345 of what? Thanks, this is bugging me.
The short answer is that the units for your area calculation are basically meaningless ([deg lat diff] * [deg lon diff]). Even though the curvature of the earth wouldn't come into play for the area calculation (since your areas are "small"), it does come into play for the calculation of distance between the lat/lon polygon coordinates.
Since a degree of longitude is different based on the distance from the equator (http://en.wikipedia.org/wiki/Longitude#Degree_length), there really is no direct conversion of your area into m^2 or km^2. It is dependent on the distance north/south of the equator.
If you always have rectangular polygons, you could just store the opposite corner coordinates and calculate area using something like this: PHP Library: Calculate a bounding box for a given lat/lng location
The most "correct" thing to do would be to store your polygons using X-Y (meters) coordinates (perhaps UTM using the WGS-84 ellipsoid), which can be calculated from lat/lon using various libraries like the following for Java: Java, convert lat/lon to UTM. You could then continue to use the MySQL AREA() function.

Finding the translation between points

I have a map of the US, and I've got a lot of pixel coordinates on that map which correspond to places of interest. These are dynamically drawn on the map at runtime according to various settings and statistics.
I now need to switch over to a different map of the US which is differently sized, and the top left corner of the map is in a slightly place in the ocean.
So I've manually collected a small set of coordinates for each map that correspond to one another. For example, the point (244,312) on the first map corresponds to the point (598,624) on the second map, and (1323,374) on the first map corresponds to (2793,545) on the second map, etc.
So I'm trying to decide the translation for the X and Y dimensions. So given a set of points for the old and new maps, how do I find the x' = A*x + C and y' = B*x + D equations to automatically translate any point from the old map to the new one?
You have the coordinates of two points on both maps, (x1,y1), (x'1, y'1), (x2, y2) and (x'2, y'2).
A = (x'1 - x'2)/(x1 - x2)
B = (y'1 - y'2)/(y1 - y2)
C = x'1 - A x1
D = y'1 - B y1
P.S. Your equations imply a simple scaling/translation from one map to another. If you're worried about using different projections from globe to plane, the equations will be more complicated.
To get result more robust against inaccuracies more that two points may help.
In this case if you assume only shift and scaling Least squares fit may help: Wikipedia
Basically you minimize sum( (Axi+B-xi')^2 + (Cyi+D-yi')^2 ) by selecting optimal A,B,C,D.