Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

geospatial queries in javascript [closed]

I'm looking for a library in javascript that would allow me to make geospatial queries. I know about OpenLayers and GoogleMaps, but this two do not support things like union intersection and so on.

+----+
|    |
|  +-+--+
+--+-+  |
   |    |
   +----+

to

    +----+
    |    |
    |    +--+
    +--+    |
       |    |
       +----+
like image 212
wolktm Avatar asked Jan 17 '11 21:01

wolktm


People also ask

What is a geospatial query?

Geospatial queries are specialized types of SQL queries supported in Athena. They differ from non-spatial SQL queries in the following ways: Using the following specialized geometry data types: point , line , multiline , polygon , and multipolygon .

Does MongoDB support geospatial?

MongoDB supports query operations on geospatial data.

Is MongoDB a spatial database?

MongoDB, which is a NoSQL Database, allows storing of Geospatial Data in the form of multiple GeoJSON types. Geospatial Feature of MongoDB makes it easy to store Geographical data into a database. So, basically, you can store the geospatial type data in the MongoDB in the form of GeoJSON objects.


3 Answers

Update 2014-04-29: Check out Turf, looks really promising

JSTS can do unions of geometric objects in the browser. JSTS integrates with the openlayers library and it extends openlayers geometric classes (e.g. OpenLayers.Geometry.Polygon) so they are capable of geometric operations. Example:

>> var poly1 = OpenLayers.Geometry.Polygon.createRegularPolygon(
                                   new OpenLayers.Geometry.Point(0.0,0.0), 2, 5);
>> var poly2 = OpenLayers.Geometry.Polygon.createRegularPolygon(
                                   new OpenLayers.Geometry.Point(1.0,1.0), 2, 4);
>> var poly_u = poly1.union(poly2);
>> var poly_d = poly1.difference(poly2);
>> print(poly_u);
POLYGON((1.5667154718422638 -0.4142135623730949,1.1755705045849463 -1.618033988749895,
  -1.1755705045849465 -1.6180339887498947,-1.902113032590307 0.618033988749895,
  -0.41421356237309503 1.6990562312593451,-0.4142135623730949 2.414213562373095,
   2.414213562373095 2.414213562373095,2.414213562373095 -0.4142135623730949,
   1.5667154718422638 -0.4142135623730949))

Geoscript JS is nice if you want to do serverside geometric operations in JS.

like image 103
ivy Avatar answered Oct 04 '22 20:10

ivy


I wrote Spatial Query https://github.com/netshade/spatial_query to do just this.

Alternatively, you could check out http://geoscript.org/index.html , which is likely better supported than Spatial Query is. If you decide to check out SQ though, I'd be flattered to hear if it worked for you.

like image 33
Chris Zelenak Avatar answered Sep 30 '22 20:09

Chris Zelenak


You can extend OpenLayers to support this operation. I make this using OpenLayers native functions. Try this, maybe you must fix and customize this code.

// The first object is instanced using data given from gmaps
var objBound1 = new OpenLayers.Bounds();
objBound1.extend(new OpenLayers.LonLat(2,2));
objBound1.extend(new OpenLayers.LonLat(8,8));

// The second object is instanced using data given from gmaps
var objBound2 = new OpenLayers.Bounds();
objBound2.extend(new OpenLayers.LonLat(5,5));
objBound2.extend(new OpenLayers.LonLat(10,10));

// Extract limits from our objects
var arrBound1 = objBound1.toArray();
var arrBound2 = objBound2.toArray();

// Determine an virtual bound. It must contain our two bounds
var intMinLeft = arrBound1.left < arrBound2.left ? arrBound1.left : arrBound2.left;
var intMinTop = arrBound1.top < arrBound2.top ? arrBound1.top : arrBound2.top;
var intMaxRight = arrBound1.right > arrBound2.right ? arrBound1.right : arrBound2.right;
var intMaxBottom = arrBound1.bottom > arrBound2.bottom ? arrBound1.bottom : arrBound2.bottom;

// Search all points of virtual bound, storing the points contained in bound1 or bound2
var objBoundResult = new OpenLayers.Bounds();
for(var intI = intMinLeft; intI < intMaxRight; intI++) {
    for(var intJ = intMinTop; intJ < intMaxBottom; intJ++) {
        if(objBound1.containsLonLat(new OpenLayers.LonLat(intI, intJ)) || objBound2.containsLonLat(new OpenLayers.LonLat(intI, intJ))) {
            objBoundResult.add(intI, intJ);
        }
    }
}

// objBoundResult is what you want
like image 29
Gustavo Costa De Oliveira Avatar answered Oct 01 '22 20:10

Gustavo Costa De Oliveira