GithubHelp home page GithubHelp logo

jede / mongoid-geo Goto Github PK

View Code? Open in Web Editor NEW

This project forked from kristianmandrup/mongoid-geo

2.0 2.0 0.0 311 KB

Geo-spatial extension for Mongoid 2

License: MIT License

JavaScript 18.87% Ruby 81.13%

mongoid-geo's Introduction

Mongoid geo

A Geo extension for Mongoid.

MongoDB Geospatial Indexing

  • Supports Mongoid 1.7 sphere distance calculations and
  • Adds nearSphere inclusion method
  • Adds a set of geo related inflections
  • Adds an exta option for defining a “geo” field, to have the generated attr_writer parse and convert strings etc. to float arrays.

Mongoid 2 geo features

Find addresses near a point

  Address.near(:latlng => [37.761523, -122.423575, 1])

Find locations within a circle

  base.where(:location.within => { "$center" => [ [ 50, -40 ], 1 ] })  

Create geo-spatial index

  class Person
    field :locations, :type => Array
    index [[ :locations, Mongo::GEO2D ]], :min => -180, :max => 180      
  end

  # to ensure indexes are created, either:
  Mongoid.autocreate_indexes = true

  # or in the mongoid.yml
  autocreate_indexes: true  

These are the only geo features I could find are currently built-in for Mongoid 2.
Mongoid Geo implements the following extra features…

Mongoid Geo features

The following briefly demonstrates all the features that Mongoid Geo currently provides

Geo index

Old/Manual way: index [[ :locations, Mongo::GEO2D ]], :min => -180, :max => 180

Using new geo_index class method : geo_index :locations

Special geo-array attribute writer

When setting a geo-location array, the setter should try to convert the value to an array of floats

Old/Manual way:

  class Person
    field :locations, :type => Array

    def locations= args
      @locations = args.kind_of?(String) ? args.split(",").map(&:to_f) : args
    end
  end  

With the new :geo option supplied by mongoid-geo :

  class Person
    field :locations, :type => Array, :geo => true
    
    geo_index :locations
  end  
  
   p = Person.new
   # set via String or Strings
   p.locations = "45.1, -3.4"
   p.locations = "45.1", "-3.4"   

   assert([45.1, -3.4], p.locations)   

Mongoid Geo extra inclusions

Find addresses near a point using spherical distance calculation

  Address.nearSphere(:locations => [ 72, -44 ])

Mongoid Geo extra inflections

nearSphere

  base.where(:locations.nearSphere => [ 72, -44 ])
  # => :locations => { "$nearSphere" : [ 72, -44 ] }

nearMax

Find points near a given point within a maximum distance

  base.where(:locations.nearMax => [[ 72, -44 ], 5])
  # => { $near: [50, 40] , $maxDistance: 3 }

  base.where(:locations.nearMax(:sphere) => [[ 72, -44 ], 5])
  # => { $nearSphere: [50, 40] , $maxDistanceSphere: 3 }

  base.where(:locations.nearMax(:sphere, :flat) => [[ 72, -44 ], 5])
  # => { $nearSphere: [50, 40] , $maxDistance: 3 }

You can also use a Hash to define the nearMax

 
  places.where(:location.nearMax => {:point => [ 72, -44 ], :distance => 5})  

Or use an Object (which must have the methods #point and #distance that return the point and max distance from that point)

  near_max_ = (Struct.new :point, :distance).new
  near_max.point = [50, 40]
  near_max.distance = [30,55]
    
  places.where(:location.nearMax => near_max)  

Note: For the points, you can also use a hash or an object with the methods/keys, either :lat, lng or :latitude, :longitude

Example:

  center = (Struct.new :lat, :lng).new
  center.lat = 72
  center.lng = -44  
  places.where(:location.withinCenter => [center, radius])    
  
  # OR
  
  places.where(:location.withinCenter => [{:lat => 72, :lng => -44}, radius])      

withinBox

  box = [[50, 40], [30,55]]
  base.where(:locations.withinBox => box)
  # => locations: {"$within" : {"$box" : [[50, 40], [30,55]]}

  base.where(:locations.withinBox(:sphere) => box)
  # => locations: {"$within" : {"$boxSphere" : [[50, 40], [30,55]]}

You can also use a Hash to define the box

 
  places.where(:location.withinBox => {:lower_left => [50, 40], :upper_right => [30,55]})  
  
  # or mix and match
  
  places.where(:location.withinBox => {:lower_left => {:lat => 50, :lng => 40}, :upper_right => [30,55] } )      

Or use an Object (which must have the methods #lower_left and #upper_right that return the points of the bounding box)

  box = (Struct.new :lower_left, :upper_right).new
  box.lower_left =  [50, 40]
  box.upper_right = [30, 55]
    
  places.where(:location.withinBox => box)  

withinCenter

  center = [50, 40]
  radius = 4

  places.where(:location.withinCenter => [center, radius])  
  # => places: {"$within" : {"$center" : [[50, 40], 4]}  

  places.where(:location.withinCenter(:sphere) => [center, radius])    
  # => places: {"$within" : {"$centerSphere" : [[50, 40], 4]}  

You can also use a Hash to define the circle

 
  places.where(:location.withinCenter => {:center => [50, 40], :radius => 4})  

Or use an Object (which must have the methods #lower_left and #upper_right that return the points of the bounding box)

  circle = (Struct.new :center, :radius).new
  circle.center = [50, 40]
  circle.radius = 4
  
  places.where(:location.withinCenter => circle)  

mongoid-geo's People

Contributors

jede avatar kristianmandrup avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.