SQLite - Index for rectangle intersection without GIS extension - sqlite

I have a quite large SQLite database to store some geo-data. I can't use the GIS extension and i'd also like everything to work with other databases as well.
My table structure is the following:
tbl(float fromLon, float fromLat, float toLon, float toLat, binary binaryData)
i store some binary-data within the database (the polygon of something) and store it's bounding-box in the form of fromLon,fromLat -> toLon,toLat.
Next i have some queries like "give me all binaryData (polygons) that are within this region (reqLlon1,reqLat1)-(reqLon2,reqLat2)".
a sample query would be:
SELECT binaryData
FROM tbl WHERE
(reqLon1 < toLon) AND
(reqLat1 < toLat) AND
(reqLon2 > fromLon) AND
(reqLat2 > fromLat)
The problem is, it seems that i can't figure the right index to speed things up.. a simple index like
idx1(fromLon,fromLat,toLon,toLat)
does not work out. I tried the same thing with MySQL (same layout, same index) and it states (explain select ..) that it only uses half of the index's length..
can someone give me a hint what the index should look like to work out? or could it be, that it's impossible?

You should consider using the R*Tree module.
An R-Tree is a special index that is designed for doing range queries.
R-Trees are most commonly used in geospatial systems where each entry
is a rectangle with minimum and maximum X and Y coordinates. Given a
query rectangle, an R-Tree is able to quickly find all entries that
are contained within the query rectangle or which overlap the query
rectangle. This idea is easily extended to three dimensions for use in
CAD systems. R-Trees also find use in time-domain range look-ups.

Related

How to delete area of polygons that overlap?

I am trying to take a single layer of polygon buffers and delete the areas in which these circular buffers overlap (or intersect... I am not sure about the correct terminology here). I have so far tried the Intersection tool and Symmetrical difference tool, but these requires two layer inputs, and I am just working with a single layer. How can I accomplish this? I am working in QGIS.
Here is what I am working with:
I simply want to select and delete the areas where these circles overlap. I have searched this extensively online, but cannot find a solution that works for me, since I am only working with one layer.
you can try select by location analysis, if select "Where the feature" condition "Overlap", it select just overlap features.enter image description here
And delete after.
You can perform SQL Query with Execute SQL tool [
https://docs.qgis.org/3.16/en/docs/user_manual/processing_algs/qgis/vectorgeneral.html#id85]
Where Additional input datasources is your circle layer, that must have unique value, in my example attribute is called fid
Query is:
select c1.fid as fid1, c2.fid as fid2, intersection(c1.geometry,c2.geometry) as geometry
from input1 as c1, input1 as c2
where c1.fid > c2.fid and st_intersects(c1.geometry,c2.geometry)
query result is intersaction between geometries:
aftert that you can do symmetrical difference to optain
There is a new QGis plugin named "Buffer Without Overlaps" that works fine!

Determine minimum number of "invisible squares" to cover all markers on a map?

I have a map full of markers corresponding to GPS coordinates, represented as a PostgreSQL + PostGIS database table using "geography" type for the GPS column.
Imagine, if you will, one semi-transparent square on top of each of these points corresponding to 1x1 mile, based from the centre, often intersecting with each other.
I'm trying to determine the minimum number of such "squares" and their GPS coordinates, so that they "cover" all of the markers with a minimum of 25 meters to the nearest border.
If it makes it any easier, the positions of these "squares" don't have to match the positions of any of the markers.
The purpose of this is to attempt to cut down the number of API requests to a "houses for sale" service significantly, since most of the positions are close to each other and the API takes a 1x1 mile square "bounding box" as the input for each call. It would be insanely wasteful to call the API many times for basically the same area when maybe 1 or 2 times would do it if I can first figure out where these imaginary "squares" go.
I get the feeling that this is considered a "known, common and solved" problem, but so far, I've not been able to figure out how to do it.
Sorry, but it seems like you have no idea what you are doing and are just being rude, both here and at PostGIS irc-channel.
You give no information about your api.
What is creating your maps?
Is it a wms-service or what?
What most people would do is setting up a mapservice with a tile cache. Then the mapservice will pich the tiles needed for each house you want to show ( or multiple houses).
The tiles will be prepared o will be created on the fly. But they will be cached for next time.
So, I think you should read up on things like
MapServer
Mapnik
MapCache
Mapproxy
GeoServer
That is not a complete list, but might give you some ideas about what it is that you want

Constrained (Delaunay) Triangulation

For a university project I need to implement a computer graphics paper that has been relased a couple of years ago. At one point, I need to triangulate the results I get from my simulation. I guess its easier to explain what I need looking at a picture contained within the paper:
Let's say I already have got all the information it takes to reconstruct the contour lines that you can see in the second thumbnail. Using those I need to do some triangulation using those siluettes as constrains. I have searched the internet for triangulation libraries like CGAL, VTK, Triangle, Triangle++, ... but I always ended up throwing my hands up in horror. I am not a good programmer and it seems impossible to me to get into one of those APIs before the deadline of this project passes.
I would appreciate any kind of help like code snipplets, tips, etc...
I know that the algorithms need segments (pairs of points) as input, so let's say I have got one std::vector containing all pairs of points defining the siluette as well as the left and right side of the rectangle.
Can you somehow give me a code snipplet for i.e. CGAL that I could use for my purpose? First of all I just want to achieve the state of the third thumbnail. Lateron I will have to do some displacement within the "cracks" and finally write the information into a VBO for OpenGL rendering.
I have started working it out with CGAL. One simple problem still drives me crazy:
It is possible to attach informations (like ints) to points before adding them up to the triangulator object. I do this since I need on the one hand an int-flag that I use lateron to define my texture coordinates and on the other hand an index which I use so that I can create a indexed VBO.
http://doc.cgal.org/latest/Triangulation_2/Triangulation_2_2info_insert_with_pair_iterator_2_8cpp-example.html
But instead of points I only want to insert constraint-edges. If I insert both CGAL returns strange results since points have been fed into two times (once as point and once as point of a constrained edge).
http://doc.cgal.org/latest/Triangulation_2/Triangulation_2_2constrained_8cpp-example.html
Is it possible to connect in the same way as with points information to "Constraints" so that I can only use this function cdt.insert_constraint( Point(j,0), Point(j,6)); before I iterate over the resulting faces?
Lateron when I loop over the triangles I need some way to access the int-flags that I defined before. Like this but not on acutal points but the "ends" defined by the constraint edges:
for(CDT::Finite_faces_iterator fit = m_cdt.finite_faces_begin(); fit != m_cdt.finite_faces_end(); ++fit, ++k) {
int j = k*3;
for(int i=0; i < 3; i++) {
indices[j+i] = fit->vertex(i)->info().first;
}
}

Fusion table layer filtering based on polygon

I've a fusion table map example with 5 layers given in this link : http://jsfiddle.net/ju2Re/
I want to filter the layers such that,
first I select a layer using dropdown : Office hierarchy > Zone > Bagalkot
Now I want to select the 220 KV stations only inside that polygon using the select box.
Please anyone help. Thank you in advance.
I don't believe you can do this natively with fusion tables (at least
not right now). The only query that can be used for "point in
polygon" analysis is:
ST_INTERSECTS(,
)
.
That takes a geometry as one argument, but the only values of are:
-
-
So if you have one point, you can use it to define a small circle and
use ST_INTERSECTS with a polygon, but that won't work for multiple
points.
With the current size of your tables you can use GViz to retrieve the
data from FusionTables and analyze it in the browser.
Proof of Concept (only works for "Geographical Heirarchy"/"District")
The code adds a "Contains" method to google.maps.Polygon, then iterates through the markers, displaying them if they are contained in the "activePolygon".
Proof of Concept should handle all the cases that have data

In PostGIS, how do I find all points within a polygon?

I am using PostgreSQL with the GIS extension to store map data, together with OpenLayers, GeoServer etc. Given a polygon, e.g. of a neighborhood, I need to find all LAT/LONG points stored in some table (e.g. traffic lights, restaurants) that are within the polygon. Alternatively given a set of polygons I would like to find the set of points within each polygon (like a GROUP BY query, rather then iterating over each polygon).
Are these functions something I need to program, or is the functionality available (as extended SQL)? Please elaborate.
Also for the simple 2D data I have do I actually need the GIS extension (GPL license is a limitation) or will PostgreSQL suffice?
Thanks!
In PostGIS you can use the bounding box operator to find candidates, which is very efficient as it uses GiST indexes. Then, if strict matches are required, use the contains operator.
Something like
SELECT
points,neighborhood_name from points_table,neighborhood
WHERE
neighborhood_poly && points /* Uses GiST index with the polygon's bounding box */
AND
ST_Contains(neighborhood_poly,points); /* Uses exact matching */
About if this is needed, depends on your requirements. For the above to work you certainly need PostGIS and GEOS installed. But, if bounding box match is enough, you can code it simply in SQL not needing PostGIS.
If exact matches are required, contains algorithms are publicly available, but to implement them efficiently requires some effort implementing it in a library which would then be called from SQL (just like GEOS).
I believe ST_Contains automatically rewrites the query to use the the GiST-indexed bounding box, as it notes:
"This function call will automatically
include a bounding box comparison that
will make use of any indexes that are
available on the geometries. To avoid
index use, use the function
_ST_Contains."
http://postgis.refractions.net/docs/ST_Contains.html
ST_Contains would solve your problem.
SELECT
polygon.gid, points.x, points.y
FROM
polygons LEFT JOIN points
ON st_contains(polygon.geom, points.geom)
WHERE
polygon.gid=your_id

Resources