Skip to content

Commit

Permalink
GeographicItem generates GeoJSON 'feature'
Browse files Browse the repository at this point in the history
  • Loading branch information
TuckerJD committed Jan 14, 2015
1 parent 8f40ed7 commit 58de280
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 4 deletions.
3 changes: 2 additions & 1 deletion app/helpers/geographic_items_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module GeographicItemsHelper
def json_tag(geographic_item)
geographic_item.to_geo_json.html_safe
retval = geographic_item.to_geo_json_feature.html_safe
retval
end

def center_coord_tag(geographic_item)
Expand Down
4 changes: 4 additions & 0 deletions app/models/geographic_area.rb
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,10 @@ def default_geographic_item
retval
end

def default_geo_json
default_geographic_item.to_geo_json2
end

def geo_object
default_geographic_item
end
Expand Down
18 changes: 15 additions & 3 deletions app/models/geographic_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def st_start_point
when :multi_polygon
retval = o[0].exterior_ring.point_n(0)
when :geometry_collection
to_geo_json =~ /(-{0,1}\d+\.{0,1}\d*),(-{0,1}\d+\.{0,1}\d*)/
to_geo_json_1 =~ /(-{0,1}\d+\.{0,1}\d*),(-{0,1}\d+\.{0,1}\d*)/
retval = Georeference::FACTORY.point($1.to_f, $2.to_f, 0.0)
else
retval = nil
Expand All @@ -145,7 +145,7 @@ def st_start_point

# Return an Array of [latitude, longitude] for the first point of GeoItem
def start_point
to_geo_json =~ /(-{0,1}\d+\.{0,1}\d*),(-{0,1}\d+\.{0,1}\d*)/
to_geo_json_1 =~ /(-{0,1}\d+\.{0,1}\d*),(-{0,1}\d+\.{0,1}\d*)/
[$2.to_f, $1.to_f]

o = st_start_point
Expand Down Expand Up @@ -532,10 +532,22 @@ def rendering_hash
data
end

def to_geo_json
def to_geo_json_1
RGeo::GeoJSON.encode(self.geo_object).to_json
end

def to_geo_json_feature
retval = {
'type' => 'Feature',
'geometry' => RGeo::GeoJSON.encode(self.geo_object),
'properties' => {
'geographic_item' => {
'id' => self.id}
}
}
retval.to_json
end

def to_a
# get our object and return the set of points as an array
we_are = self.geo_object
Expand Down
60 changes: 60 additions & 0 deletions lib/gis/geo_json.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
module Gis::GeoJSON
# not tested

=begin
{ "type": "FeatureCollection",
"features": [
{ "type": "Feature",
"geometry": {"type": "Point", "coordinates": [102.0, 0.5]},
"properties": {"prop0": "value0"}
},
{ "type": "Feature",
"geometry": {
"type": "LineString",
"coordinates": [
[102.0, 0.0], [103.0, 1.0], [104.0, 0.0], [105.0, 1.0]
]
},
"properties": {
"prop0": "value0",
"prop1": 0.0
}
},
{ "type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0],
[100.0, 1.0], [100.0, 0.0] ]
]
},
"properties": {
"prop0": "value0",
"prop1": {"this": "that"}
}
}
]
}
=end

# @param objects [Array of instances that respond to .to_geo_json_1]
def self.feature_collection(objects)
result = {
'type' => 'FeatureCollection',
'features' => []
}
objects.each_with_index do |o, i|
json = o.to_geo_json2.merge('id' => i)
result['features'].push json
end
result
end

# # @return [a Feature]
# def to_geo_json_using_entity_factory
# f = RGeo::GeoJSON::EntityFactory.new
# inserted_attributes = {foo: "bar"} # some of self.attributes, but not all
# f.feature(self.geo_object, self.id, inserted_attributes)
# end

end
1 change: 1 addition & 0 deletions spec/support/geo/geo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,7 @@ def generate_geo_test_objects
puts
@debug_names.collect { |k, v| print "#{' ' * 4}" + v.to_s + ": " + k.to_s }
puts @debug_names.invert[@p1]

end
@debug_names
end
Expand Down

0 comments on commit 58de280

Please sign in to comment.