Note that there are some explanatory texts on larger screens.

plurals
  1. POConstucting a PostGIS query with Rails
    primarykey
    data
    text
    <p>I'm using PostGIS (1.5.1) with Rails (3.0.0rc) and a Google Map (v3), and I'm looking for a way to query the database to retrieve all the points within a polygon (specifically, the bounds of the map viewport). At present I am sending the coordinates of the bounds to the server as such:</p> <pre><code>google.maps.event.addListener(map, 'bounds_changed', function() { var bounds = map.getBounds(); var ne = bounds.getNorthEast(); var sw = bounds.getSouthWest(); var yMaxLat = ne.lat(); var xMaxLng = ne.lng(); var yMinLat = sw.lat(); var xMinLng = sw.lng(); //alert("bounds changed"); updateMap(yMaxLat, xMaxLng, yMinLat, xMinLng); }); function updateMap(yMaxLat, xMaxLng, yMinLat, xMinLng) { jQuery.getJSON("/places", { "yMaxLat": yMaxLat, "xMaxLng": xMaxLng, "yMinLat": yMinLat, "xMinLng": xMinLng }, function(json) { if (json.length &gt; 0) { for (i=0; i&lt;json.length; i++) { var place = json[i]; var category = json[i].tag; addLocation(place,category); } } }); } </code></pre> <p>Then in my controller:</p> <pre><code>format.js do xMinLng = params[:xMinLng] yMinLat = params[:yMinLat] xMaxLng = params[:xMaxLng] yMaxLat = params[:yMaxLat] @map_places = Place.find(:all, :conditions =&gt; ["geom &amp;&amp; ?", Polygon.from_coordinates([[[xMinLng, yMinLat], [xMinLng, yMaxLat], [xMaxLng, yMaxLat], [xMaxLng, yMinLat], [xMinLng, yMinLat]]], 4326)]) render :json =&gt; @map_places end </code></pre> <p>I'm basing my query on <a href="http://railsonpostgresql.com/2009/11/05/intro-to-postgis" rel="nofollow noreferrer">this example</a>, but it isn't really behaving as expected - most points are shown when the map is zoomed right out, but not those nearish to the international date line (ie. in Australia and New Zealand). The points aren't shown at all when the map is zoomed in, for any region of the globe. I've read something about PostGIS's geography type being more appropriate for this kind of query, but I don't <em>think</em> that such a type is supported by GeoRuby (it is supported by GeoDjango). The output from my console for such a request is below:</p> <pre><code>Started GET "/places?yMaxLat=63.93767251746141&amp;xMaxLng=43.033828125&amp;yMinLat=36.88016688406946&amp;xMinLng=-39.407578125" for 127.0.0.1 at Tue Aug 24 12:11:41 +0100 2010 Processing by PlacesController#index as JSON Parameters: {"xMinLng"=&gt;"-39.407578125", "yMaxLat"=&gt;"63.93767251746141", "xMaxLng"=&gt;"43.033828125", "yMinLat"=&gt;"36.88016688406946"} Place Load (0.4ms) SELECT "places".* FROM "places" Place Load (0.8ms) SELECT "places".* FROM "places" WHERE (geom &amp;&amp; '0103000020E6100000010000000500000052B81E852BB443C0DF0CF74EA970424052B81E852BB443C0686D2EA705F84F40AE47E17A54844540686D2EA705F84F40AE47E17A54844540DF0CF74EA970424052B81E852BB443C0DF0CF74EA9704240') Completed 200 OK in 136ms (Views: 3.4ms | ActiveRecord: 1.2ms) </code></pre> <p>What is the best way to go about constructing such a query? I'm using the GeoRuby and spatial_adapter gems with my project, but I'm struggling to find much information on using them as I've described above. Would a distance query be the better approach to take perhaps? Thanks!</p> <p>EDIT: my migration is as below:</p> <pre><code>class CreatePlaces &lt; ActiveRecord::Migration def self.up create_table :places do |t| t.string :city_name t.integer :country_id, :null =&gt; false t.point :geom, :null =&gt; false, :with_z =&gt; true, :srid =&gt; 4326 t.string :name, :null =&gt; false t.string :tag, :null =&gt; false t.timestamps end add_index :places, :geom, :spatial =&gt; true end def self.down drop_table :places end remove_index :places, :geom end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload