Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1065591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T19:49:28+00:00 2026-05-16T19:49:28+00:00

I’m using PostGIS (1.5.1) with Rails (3.0.0rc) and a Google Map (v3), and I’m

  • 0

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:

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 > 0) {
      for (i=0; i<json.length; i++) {
        var place = json[i];
        var category = json[i].tag;
        addLocation(place,category);
      }
    }
  });
}

Then in my controller:

format.js do
  xMinLng = params[:xMinLng]
  yMinLat = params[:yMinLat]
  xMaxLng = params[:xMaxLng]
  yMaxLat = params[:yMaxLat]

  @map_places = Place.find(:all, :conditions => ["geom && ?", Polygon.from_coordinates([[[xMinLng, yMinLat], [xMinLng, yMaxLat], [xMaxLng, yMaxLat], [xMaxLng, yMinLat], [xMinLng, yMinLat]]], 4326)])
  render :json => @map_places
end

I’m basing my query on this example, 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 think that such a type is supported by GeoRuby (it is supported by GeoDjango). The output from my console for such a request is below:

Started GET "/places?yMaxLat=63.93767251746141&xMaxLng=43.033828125&yMinLat=36.88016688406946&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"=>"-39.407578125", "yMaxLat"=>"63.93767251746141", "xMaxLng"=>"43.033828125", "yMinLat"=>"36.88016688406946"}
Place Load (0.4ms)  SELECT "places".* FROM "places"
Place Load (0.8ms)  SELECT "places".* FROM "places" WHERE (geom && '0103000020E6100000010000000500000052B81E852BB443C0DF0CF74EA970424052B81E852BB443C0686D2EA705F84F40AE47E17A54844540686D2EA705F84F40AE47E17A54844540DF0CF74EA970424052B81E852BB443C0DF0CF74EA9704240')
Completed 200 OK in 136ms (Views: 3.4ms | ActiveRecord: 1.2ms)

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!

EDIT: my migration is as below:

class CreatePlaces < ActiveRecord::Migration
  def self.up
    create_table :places do |t|
      t.string :city_name
      t.integer :country_id, :null => false
      t.point :geom, :null => false, :with_z => true, :srid => 4326
      t.string :name, :null => false
      t.string :tag, :null => false

      t.timestamps
    end
add_index :places, :geom, :spatial => true
  end

  def self.down
    drop_table :places
  end
  remove_index :places, :geom
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T19:49:29+00:00Added an answer on May 16, 2026 at 7:49 pm

    Thanks for all the input – I’m afraid this actually turned out to be a massive n00b error on my part, in getting the correlation between x/y and lng/lat mixed up, hence why the points on the map showed fine when zoomed out, but disappeared when zoomed in. I’d thoroughly recommend QGIS (thanks to scw on https://gis.stackexchange.com/ for suggesting it) for anyone starting out with GIS, I immediately saw the error when the points were shown upside down!! Thanks again all!

    BTW, slightly surprisingly, the points in NZ/Australia show up fine even when the bounding box crosses the international date line, although that might change if I add some more points in that area.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
We're building an app, our first using Rails 3, and we're having to build
I'm making a simple page using Google Maps API 3. My first. One marker
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.