Skip to content

Commit

Permalink
GeographicItem.containing; functioning for single or multiple objects.
Browse files Browse the repository at this point in the history
  • Loading branch information
TuckerJD committed Mar 28, 2014
1 parent 6ef50bf commit 3796f7b
Showing 1 changed file with 30 additions and 9 deletions.
39 changes: 30 additions & 9 deletions app/models/geographic_item.rb
Expand Up @@ -3,6 +3,8 @@ class GeographicItem < ActiveRecord::Base
# RGeo::Geos.preferred_native_interface = :ffi
# RGeo::ActiveRecord::GeometryMixin.set_json_generator(:geojson)

# include RGeo::ActiveRecord

include Housekeeping::Users
# include ActiveRecordSpatial::SpatialColumns
# include ActiveRecordSpatial::SpatialScopes
Expand All @@ -17,11 +19,11 @@ class GeographicItem < ActiveRecord::Base
:multi_polygon,
:geometry_collection]

# column_factory = RGeo::Geos.factory(
# native_interface: :ffi,
# srid: 4326,
# has_z_coordinate: true,
# has_m_coordinate: false)
# column_factory = RGeo::Geos.factory(
# native_interface: :ffi,
# srid: 4326,
# has_z_coordinate: true,
# has_m_coordinate: false)

column_factory = Georeference::FACTORY

Expand Down Expand Up @@ -108,25 +110,42 @@ def self.disjoint_from(column_name, geographic_items)

end

# If this method is given an Array of GeographicItems as a second parameter, it will return the 'or' of each of the
def self.disjoint_from_sql(column_name, geographic_items)
# where{"ST_Disjoint(#{column_name}::geometry, ST_GeomFromText('srid=4326;#{geographic_item.geo_object}'))"}

if check_geo_params(column_name, geographic_item)
"ST_Contains(#{column_name}::geometry, ST_GeomFromText('srid=4326;#{geographic_item.geo_object}'))"
else
'false'
end

end

# if this method is given an array of GeographicItems as a second parameter, it will return the 'or' of each of the
# objects against the table
def self.containing(column_name, *geographic_items)
# where{"ST_contains(#{column_name}::geometry, ST_GeomFromText('srid=4326;POINT(-29 -16)'))"}

where { geographic_items.flatten.collect { |geographic_item| GeographicItem.containing_sql(column_name, geographic_item) }.join(' or ') }
end

def self.containing_sql(column_name, geographic_item)
# where{"ST_contains(#{column_name}::geometry, ST_GeomFromText('srid=4326;POINT(-29 -16)'))"}
# was ST_GeomFromText
check_geo_params(column_name, geographic_item) ?
"ST_Contains(#{column_name}::geometry, GeomFromEWKT('srid=4326;#{geographic_item.geo_object}'))" :

if check_geo_params(column_name, geographic_item)
"ST_Contains(#{column_name}::geometry, ST_GeomFromText('srid=4326;#{geographic_item.geo_object}'))"
else
'false'
end

end

def self.ordered_by_shortest_distance_from(column_name, geographic_item)

end

def self.ordered_by_longest_distance_from(column_name, geographic_item)

end

=begin
Expand All @@ -150,6 +169,7 @@ def self.clean?
GeographicItem.connection.execute('delete from t20140306 where id in (select geographic_item_id from georeferences where geographic_item_id is not null)')

list = GeographicItem.connection.execute('select id from t20140306').to_a

end

def self.clean!
Expand Down Expand Up @@ -200,6 +220,7 @@ def data_type?
# lines: [],
# polygons: []
# }

def rendering_hash
data = {}
if self.geo_object
Expand Down

0 comments on commit 3796f7b

Please sign in to comment.