Class: MapPLZ
- Inherits:
-
Object
- Object
- MapPLZ
- Defined in:
- lib/mapplz.rb
Overview
MapPLZ datastore
Defined Under Namespace
Classes: GeoItem, MapPLZException
Constant Summary collapse
- DATABASES =
%w(array postgres postgresql postgis sqlite spatialite mongodb)
Class Method Summary collapse
- .flip_path(path) ⇒ Object
- .parse_wkt(geo_item, geom_string) ⇒ Object
- .standardize_geo(user_geo, lonlat = false, db = nil) ⇒ Object
Instance Method Summary collapse
-
#<<(user_geo) ⇒ Object
aliases for add.
- #add(user_geo, lonlat = false) ⇒ Object
- #choose_db(db) ⇒ Object
- #code(mapplz_code) ⇒ Object
- #count(where_clause = nil, add_on = nil) ⇒ Object
-
#embed_html(options = {}) ⇒ Object
aliases for render_html.
-
#initialize(db = {}) ⇒ MapPLZ
constructor
A new instance of MapPLZ.
- #inside(user_geo) ⇒ Object
- #length(where_clause = nil, add_on = nil) ⇒ Object
- #near(user_geo, limit = 10, max = 40_010_000, lon_lat = false) ⇒ Object
- #push(user_geo) ⇒ Object
- #query(where_clause = nil, add_on = nil) ⇒ Object
- #render_html(options = {}) ⇒ Object
-
#size(where_clause = nil, add_on = nil) ⇒ Object
aliases for count.
- #to_geojson ⇒ Object
-
#where(where_clause = nil, add_on = nil) ⇒ Object
aliases for query.
Constructor Details
#initialize(db = {}) ⇒ MapPLZ
Returns a new instance of MapPLZ.
14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/mapplz.rb', line 14 def initialize(db = {}) @db_type = 'array' @db_client = db @db = { client: @db_client, type: @db_type } @my_array = [] @parser = SqlParser.new choose_db(ActiveRecord::Base.connection.adapter_name) if defined?(ActiveRecord) end |
Class Method Details
.flip_path(path) ⇒ Object
308 309 310 311 312 |
# File 'lib/mapplz.rb', line 308 def self.flip_path(path) path.map! do |pt| [pt[1].to_f, pt[0].to_f] end end |
.parse_wkt(geo_item, geom_string) ⇒ Object
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 |
# File 'lib/mapplz.rb', line 314 def self.parse_wkt(geo_item, geom_string) if geom_string.index('POINT') coordinates = geom_string.gsub('POINT', '').gsub('(', '').gsub(')', '').split(' ') geo_item[:lat] = coordinates[1].to_f geo_item[:lng] = coordinates[0].to_f elsif geom_string.index('LINESTRING') line_nodes = geom_string.gsub('LINESTRING', '').gsub('(', '').gsub(')', '').split(',') geo_item[:path] = line_nodes.map do |pt| pt = pt.split(' ') [pt[1].to_f, pt[0].to_f] end elsif geom_string.index('POLYGON') line_nodes = geom_string.gsub('POLYGON', '').gsub('(', '').gsub(')', '').split(', ') geo_item[:path] = line_nodes.map do |pt| pt = pt.split(' ') [pt[1].to_f, pt[0].to_f] end end geo_item end |
.standardize_geo(user_geo, lonlat = false, db = nil) ⇒ Object
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 |
# File 'lib/mapplz.rb', line 335 def self.standardize_geo(user_geo, lonlat = false, db = nil) return [user_geo] if user_geo.is_a?(GeoItem) geo_objects = [] if user_geo.is_a?(File) file_type = File.extname(user_geo) if ['.csv', '.tsv', '.tdv', '.txt', '.geojson', '.json'].include?(file_type) # parse this as if it were sent as a string user_geo = user_geo.read else # convert this with ogr2ogr and parse as a string begin `ogr2ogr -f "GeoJSON" tmp.geojson #{File.path(user_geo)}` user_geo = File.open('tmp.geojson').read rescue raise 'gdal was not installed, or format was not accepted by ogr2ogr' end end end if user_geo.is_a?(String) begin user_geo = JSON.parse(user_geo) rescue # not JSON - attempt CSV begin CSV.parse(user_geo.gsub('\"', '""'), headers: true) do |row| geo_objects += standardize_geo(row, lonlat, db) end return geo_objects rescue # not JSON or CSV - attempt mapplz parse raise MapPLZException, 'call code() to parse mapplz language' end end end if user_geo.is_a?(Array) && user_geo.length > 0 if user_geo[0].is_a?(Array) && user_geo[0].length > 0 if user_geo[0][0].is_a?(Array) || (user_geo[0][0].is_a?(Hash) && user_geo[0][0].key?(:lat) && user_geo[0][0].key?(:lng)) # lines and shapes user_geo.map! do |path| path_pts = [] path.each do |path_pt| if lonlat lat = path_pt[1] || path_pt[:lat] lng = path_pt[0] || path_pt[:lng] else lat = path_pt[0] || path_pt[:lat] lng = path_pt[1] || path_pt[:lng] end path_pts << [lat, lng] end # polygon border repeats first point if path_pts[0] == path_pts.last geo_type = 'polygon' path_pts = [path_pts] else geo_type = 'polyline' end geoitem = GeoItem.new(db) geoitem[:path] = path_pts geoitem[:type] = geo_type geoitem end return user_geo end end # multiple objects being added? iterate through if user_geo[0].is_a?(Hash) || user_geo[0].is_a?(Array) user_geo.each do |geo_piece| geo_objects += standardize_geo(geo_piece, lonlat, db) end return geo_objects end # first two spots are a coordinate validate_lat = user_geo[0].to_f != 0 || user_geo[0].to_s == '0' validate_lng = user_geo[1].to_f != 0 || user_geo[1].to_s == '0' if validate_lat && validate_lng geo_object = GeoItem.new(db) geo_object[:type] = 'point' if lonlat geo_object[:lat] = user_geo[1].to_f geo_object[:lng] = user_geo[0].to_f else geo_object[:lat] = user_geo[0].to_f geo_object[:lng] = user_geo[1].to_f end else fail 'no latitude or longitude found' end # assume user properties are an ordered array of values known to the user user_properties = user_geo.drop(2) # only one property and it's a string? check for JSON hash if user_properties.length == 1 && user_properties[0].is_a?(String) begin user_properties[0] = JSON.parse(user_properties[0]) rescue end end # only one property and it's a hash? it's a hash of properties if user_properties.length == 1 && user_properties[0].is_a?(Hash) user_properties[0].keys.each do |key| geo_object[key.to_sym] = user_properties[0][key] end else geo_object[:properties] = user_properties end geo_objects << geo_object elsif user_geo.is_a?(Hash) || user_geo.is_a?(CSV::Row) if user_geo.is_a?(CSV::Row) # convert CSV::Row to geo hash geo_hash = {} user_geo.headers.each do |header| geo_hash[header] = user_geo[header] end user_geo = geo_hash end # check for lat and lng validate_lat = false validate_lat = 'lat' if user_geo.key?('lat') || user_geo.key?(:lat) validate_lat ||= 'latitude' if user_geo.key?('latitude') || user_geo.key?(:latitude) validate_lng = false validate_lng = 'lng' if user_geo.key?('lng') || user_geo.key?(:lng) validate_lng ||= 'lon' if user_geo.key?('lon') || user_geo.key?(:lon) validate_lng ||= 'long' if user_geo.key?('long') || user_geo.key?(:long) validate_lng ||= 'longitude' if user_geo.key?('longitude') || user_geo.key?(:longitude) if validate_lat && validate_lng # single hash geo_object = GeoItem.new(db) geo_object[:lat] = user_geo[validate_lat].to_f geo_object[:lng] = user_geo[validate_lng].to_f geo_object[:type] = 'point' user_geo.keys.each do |key| next if key == validate_lat || key == validate_lng geo_object[key.to_sym] = user_geo[key] end geo_objects << geo_object elsif user_geo.key?('path') || user_geo.key?(:path) # try line or polygon path_pts = [] path = user_geo['path'] if user_geo.key?('path') path = user_geo[:path] if user_geo.key?(:path) if path_pts[0].is_a?(Array) && path_pts[0][0].is_a?(Array) # ring polygon path.each do |ring| ring.map! do |path_pt| if lonlat lat = path_pt[1] || path_pt[:lat] lng = path_pt[0] || path_pt[:lng] else lat = path_pt[0] || path_pt[:lat] lng = path_pt[1] || path_pt[:lng] end [lat, lng] end end path_pts = path else path.each do |path_pt| if lonlat lat = path_pt[1] || path_pt[:lat] lng = path_pt[0] || path_pt[:lng] else lat = path_pt[0] || path_pt[:lat] lng = path_pt[1] || path_pt[:lng] end path_pts << [lat, lng] end # polygon border repeats first point if path_pts[0] == path_pts.last geo_type = 'polygon' path_pts = [path_pts] else geo_type = 'polyline' end end geoitem = GeoItem.new(db) geoitem[:path] = path_pts geoitem[:type] = geo_type property_list = user_geo.clone property_list = property_list[:properties] if property_list.key?(:properties) property_list = property_list['properties'] if property_list.key?('properties') property_list.delete(:path) property_list.keys.each do |prop| geoitem[prop.to_sym] = property_list[prop] end geo_objects << geoitem elsif user_geo.key?('geo') || user_geo.key?(:geo) # this key is GeoJSON or WKT geotext = (user_geo['geo'] || user_geo[:geo]) if geotext.upcase.index('POINT(') || geotext.upcase.index('LINESTRING(') || geotext.upcase.index('POLYGON(') # try WKT geoitem = GeoItem.new(db) geoitem = MapPLZ.parse_wkt(geoitem, geotext) else # GeoJSON begin geoitem = standardize_geo(JSON.parse(geotext), lonlat, db)[0] rescue # did not recognize format raise 'did not recognize format in CSV geo column' end end user_geo.keys.each do |key| next if ['lat', 'lng', 'geo', 'geom', 'geojson', 'wkt', :lat, :lng, :geo, :geom, :geojson, :wkt].include?(key) geoitem[key.to_sym] = user_geo[key] end geo_objects << geoitem else # try GeoJSON if user_geo.key?(:type) user_geo['type'] = user_geo[:type] || '' user_geo['features'] = user_geo[:features] if user_geo.key?(:features) user_geo['properties'] = user_geo[:properties] || {} if user_geo.key?(:geometry) user_geo['geometry'] = user_geo[:geometry] user_geo['geometry']['type'] = user_geo[:geometry][:type] user_geo['geometry']['coordinates'] = user_geo[:geometry][:coordinates] end end if user_geo.key?('type') if user_geo['type'] == 'FeatureCollection' && user_geo.key?('features') # recursive onto features user_geo['features'].each do |feature| geo_objects += standardize_geo(feature, lonlat, db) end elsif user_geo.key?('geometry') && user_geo['geometry'].key?('coordinates') # each feature coordinates = user_geo['geometry']['coordinates'] if user_geo['geometry']['type'] == 'Point' geo_object = GeoItem.new(db) geo_object[:lat] = coordinates[1].to_f geo_object[:lng] = coordinates[0].to_f geo_object[:type] = 'point' geo_objects << geo_object elsif user_geo['geometry']['type'] == 'LineString' geo_object = GeoItem.new(db) MapPLZ.flip_path(coordinates) geo_object[:path] = coordinates geo_object[:type] = 'polyline' geo_objects << geo_object elsif user_geo['geometry']['type'] == 'Polygon' geo_object = GeoItem.new(db) coordinates.map! do |ring| MapPLZ.flip_path(ring) end geo_object[:path] = coordinates geo_object[:type] = 'polygon' geo_objects << geo_object elsif user_geo['geometry']['type'] == 'MultiPoint' coordinates.each do |point| geo_object = GeoItem.new(db) geo_object[:lat] = point[1].to_f geo_object[:lng] = point[0].to_f geo_object[:type] = 'point' geo_objects << geo_object end elsif user_geo['geometry']['type'] == 'MultiLineString' coordinates.each do |line| geo_object = GeoItem.new(db) geo_object[:path] = MapPLZ.flip_path(line) geo_object[:type] = 'polyline' geo_objects << geo_object end elsif user_geo['geometry']['type'] == 'MultiPolygon' coordinates.each do |poly| geo_object = GeoItem.new(db) poly.map! do |ring| MapPLZ.flip_path(ring) end geo_object[:path] = poly geo_object[:type] = 'polygon' geo_objects << geo_object end end # store properties on all generated geometries prop_keys = {} if user_geo.key?('properties') user_geo['properties'].keys.each do |key| prop_keys[key.to_sym] = user_geo['properties'][key] end end geo_objects.each do |geo| prop_keys.keys.each do |key| geo[key] = prop_keys[key] end end end end end end geo_objects end |
Instance Method Details
#<<(user_geo) ⇒ Object
aliases for add
656 657 658 |
# File 'lib/mapplz.rb', line 656 def <<(user_geo) add(user_geo) end |
#add(user_geo, lonlat = false) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/mapplz.rb', line 38 def add(user_geo, lonlat = false) begin geo_objects = MapPLZ.standardize_geo(user_geo, lonlat, @db) rescue MapPLZException geo_objects = code(user_geo) end if @db_type == 'array' @my_array += geo_objects elsif @db_type == 'mongodb' geo_objects.each do |geo_object| save_obj = geo_object.clone save_obj.delete(:_id) save_obj.delete(:lat) save_obj.delete(:lng) save_obj.delete(:path) save_obj.delete(:centroid) save_obj.delete(:type) save_obj[:geo] = JSON.parse(geo_object.to_geojson)['geometry'] reply = @db_client.insert(save_obj) geo_object[:_id] = reply.to_s end elsif @db_type == 'postgis' || @db_type == 'spatialite' geo_objects.each do |geo_object| geom = geo_object.to_wkt if @db_type == 'postgis' geojson_props = (JSON.parse(geo_object.to_geojson)['properties'] || {}) reply = @db_client.exec("INSERT INTO mapplz (properties, geom) VALUES ('#{geojson_props.to_json}', ST_GeomFromText('#{geom}')) RETURNING id") elsif @db_type == 'spatialite' reply = @db_client.execute("INSERT INTO mapplz (label, geom) VALUES ('#{geo_object[:label] || ''}', AsText('#{geom}')) RETURNING id") end geo_object[:id] = reply[0]['id'] end end if geo_objects.length == 1 geo_objects[0] else geo_objects end end |
#choose_db(db) ⇒ Object
28 29 30 31 32 33 34 35 36 |
# File 'lib/mapplz.rb', line 28 def choose_db(db) db.downcase! fail 'Database type not supported by MapPLZ' unless DATABASES.include?(db) db = 'postgis' if db == 'postgres' || db == 'postgresql' db = 'spatialite' if db == 'sqlite' db = 'mongodb' if db == 'mongo' @db_type = db @db[:type] = db end |
#code(mapplz_code) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/mapplz.rb', line 140 def code(mapplz_code) @code_lines = mapplz_code.gsub("\r", '').split("\n") @code_level = 'toplevel' @button_layers = [] @code_button = 0 @code_layers = [] @code_label = '' @code_color = nil code_line(0) @code_layers end |
#count(where_clause = nil, add_on = nil) ⇒ Object
80 81 82 83 |
# File 'lib/mapplz.rb', line 80 def count(where_clause = nil, add_on = nil) results = query(where_clause, add_on) results.length end |
#embed_html(options = {}) ⇒ Object
aliases for render_html
679 680 681 |
# File 'lib/mapplz.rb', line 679 def ( = {}) render_html() end |
#inside(user_geo) ⇒ Object
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 |
# File 'lib/mapplz.rb', line 274 def inside(user_geo) geo_results = [] # accept [point1, point2, point3, point1] as a polygon search area if user_geo.is_a?(Array) && user_geo[0].is_a?(Array) && !user_geo[0][0].is_a?(Array) user_geo = [user_geo] end search_areas = MapPLZ.standardize_geo(user_geo) search_areas.each do |search_area| next unless search_area.key?(:path) wkt = search_area.to_wkt if @db_type == 'array' # in-Ruby point-in-polygon @my_array.each do |geo_item| GeoItem.centroid(geo_item) geo_results << geo_item if geo_item.inside?(search_area) end elsif @db_type == 'mongodb' polygon_gj = JSON.parse(search_area.to_geojson)['geometry'] cursor = @db_client.find(geo: { '$geoWithin' => { '$geometry' => polygon_gj } }) elsif @db_type == 'postgis' cursor = @db_client.exec("SELECT id, ST_AsText(geom) AS geo, properties FROM mapplz AS start WHERE ST_Contains(ST_GeomFromText('#{wkt}'), start.geom)") elsif @db_type == 'spatialite' cursor = @db_client.exec("SELECT id, AsText(geom) AS geo, label FROM mapplz WHERE MBRContains(FromText('#{wkt}'), FromText(geom))") end geo_results += read_cursor(cursor) end geo_results end |
#length(where_clause = nil, add_on = nil) ⇒ Object
674 675 676 |
# File 'lib/mapplz.rb', line 674 def length(where_clause = nil, add_on = nil) count(where_clause, add_on) end |
#near(user_geo, limit = 10, max = 40_010_000, lon_lat = false) ⇒ Object
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
# File 'lib/mapplz.rb', line 236 def near(user_geo, limit = 10, max = 40_010_000, lon_lat = false) max = max.to_f # send max in meters limit = limit.to_i if user_geo.is_a?(Hash) lat = user_geo[:lat] || user_geo['lat'] || user_geo[:latitude] || user_geo['latitude'] lng = user_geo[:lng] || user_geo['lng'] || user_geo[:longitude] || user_geo['longitude'] user_geo = [lat.to_f, lng.to_f] elsif user_geo.is_a?(Array) user_geo.reverse! if lon_lat else fail 'must query near a point' end lat = user_geo[0].to_f lng = user_geo[1].to_f wkt = "POINT(#{lng} #{lat})" geo_results = [] if @db_type == 'array' @my_array.sort! do |a, b| a_distance = a.distance_from([lat, lng]) b_distance = b.distance_from([lat, lng]) a_distance <=> b_distance end geo_results = @my_array.slice(0, limit) elsif @db_type == 'mongodb' cursor = @db_client.find(geo: { '$nearSphere' => { '$geometry' => { type: 'Point', coordinates: [lng, lat] }, '$maxDistance' => max } }) elsif @db_type == 'postgis' cursor = @db_client.exec("SELECT id, ST_AsText(geom) AS geo, properties, ST_Distance(start.geom::geography, ST_GeomFromText('#{wkt}')::geography) AS distance FROM mapplz AS start WHERE ST_Distance(start.geom::geography, ST_GeomFromText('#{wkt}')::geography) <= #{max} ORDER BY distance LIMIT #{limit}") elsif @db_type == 'spatialite' cursor = @db_client.execute("SELECT id, AsText(geom) AS geo, label, Distance(start.geom, AsText('#{wkt}')) AS distance FROM mapplz AS start WHERE Distance(start.geom, AsText('#{wkt}')) <= #{max} ORDER BY distance LIMIT #{limit}") end geo_results += read_cursor(cursor) geo_results end |
#push(user_geo) ⇒ Object
660 661 662 |
# File 'lib/mapplz.rb', line 660 def push(user_geo) add(user_geo) end |
#query(where_clause = nil, add_on = nil) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/mapplz.rb', line 85 def query(where_clause = nil, add_on = nil) geo_results = [] if where_clause.present? if @db_type == 'array' geo_results = query_array(where_clause, add_on) elsif @db_type == 'mongodb' conditions = parse_sql(where_clause, add_on = nil) mongo_conditions = {} conditions.each do |condition| field = condition[:field] compare_value = add_on || condition[:value] operator = condition[:operator].to_s mongo_conditions[field] = compare_value if operator == '=' mongo_conditions[field] = { '$lt' => compare_value } if operator == '<' mongo_conditions[field] = { '$lte' => compare_value } if operator == '<=' mongo_conditions[field] = { '$gt' => compare_value } if operator == '>' mongo_conditions[field] = { '$gte' => compare_value } if operator == '>=' end cursor = @db_client.find(mongo_conditions) elsif @db_type == 'postgis' where_prop = where_clause.strip.split(' ')[0] where_clause = where_clause.gsub(where_prop, "json_extract_path_text(properties, '#{where_prop}')") cursor = @db_client.exec("SELECT id, ST_AsText(geom) AS geo, properties FROM mapplz WHERE #{where_clause}") elsif @db_type == 'spatialite' if add_on.is_a?(String) where_clause = where_clause.gsub('?', "'#{add_on}'") elsif add_on.is_a?(Integer) || add_on.is_a?(Float) where_clause = where_clause.gsub('?', "#{add_on}") end cursor = @db_client.execute("SELECT id, AsText(geom) AS geo, label FROM mapplz WHERE #{where_clause}") end else # query all if @db_type == 'array' geo_results = @my_array elsif @db_type == 'mongodb' cursor = @db_client.find elsif @db_type == 'postgis' cursor = @db_client.exec('SELECT id, ST_AsText(geom) AS geo, properties FROM mapplz') elsif @db_type == 'spatialite' cursor = @db_client.execute('SELECT id, AsText(geom) AS geo, label FROM mapplz') else # @my_db.all end end geo_results += read_cursor(cursor) geo_results end |
#render_html(options = {}) ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/mapplz.rb', line 157 def render_html( = {}) # Leaflet options [:tile_layer] ||= Leaflet.tile_layer [:attribution] ||= Leaflet.attribution [:max_zoom] ||= Leaflet.max_zoom [:container_id] ||= 'map' geojson_features = JSON.parse(to_geojson)['features'] # use Leaflet to add clickable markers [:markers] = [] geojson_features.each do |feature| next if feature['geometry']['type'] != 'Point' if feature.key?('properties') if feature['properties'].is_a?(Hash) && feature['properties'].key?('label') label = feature['properties']['label'] elsif feature['properties'].key?('properties') && feature['properties']['properties'].is_a?(Array) && feature['properties']['properties'].length == 1 label = feature['properties']['properties'][0] end else label = nil end [:markers] << { latlng: feature['geometry']['coordinates'].reverse, popup: label } end render_text = map().gsub('</script>', '') # add clickable lines and polygons after # Leaflet-Rails does not support clickable lines or any polygons geojson_features.each do |feature| next if feature['geometry']['type'] == 'Point' label = nil = {} [:color] = [:color] if .key?(:color) [:opacity] = [:opacity] if .key?(:opacity) [:fillColor] = [:fillColor] if .key?(:fillColor) [:fillOpacity] = [:fillOpacity] if .key?(:fillOpacity) [:weight] = [:weight] if .key?(:weight) [:stroke] = [:stroke] if .key?(:stroke) if feature.key?('properties') if feature['properties'].key?('label') label = feature['properties']['label'] elsif feature['properties'].key?('properties') && feature['properties']['properties'].is_a?(Array) && feature['properties']['properties'].length == 1 label = feature['properties']['properties'][0] end [:color] = feature['properties']['color'] if feature['properties'].key?('color') [:opacity] = feature['properties']['opacity'].to_f if feature['properties'].key?('opacity') [:fillColor] = feature['properties']['fillColor'] if feature['properties'].key?('fillColor') [:fillOpacity] = feature['properties']['fillOpacity'].to_f if feature['properties'].key?('fillOpacity') [:weight] = feature['properties']['weight'].to_i if feature['properties'].key?('weight') [:stroke] = feature['properties']['stroke'] if feature['properties'].key?('stroke') [:clickable] = true unless label.nil? end flip_coordinates = feature['geometry']['coordinates'] if flip_coordinates[0][0].is_a?(Array) flip_coordinates.each do |segment| segment.map! { |coord| coord.reverse } end else flip_coordinates.map! { |coord| coord.reverse } end if feature['geometry']['type'] == 'LineString' render_text += ('line = L.polyline(' + flip_coordinates.to_json + ", #{.to_json}).addTo(map);\n").html_safe render_text += "line.bindPopup('#{label}');\n".html_safe unless label.nil? elsif feature['geometry']['type'] == 'Polygon' render_text += ('polygon = L.polygon(' + flip_coordinates[0].to_json + ", #{.to_json}).addTo(map);\n").html_safe render_text += "polygon.bindPopup('#{label}');\n".html_safe unless label.nil? end end render_text + '</script>' end |
#size(where_clause = nil, add_on = nil) ⇒ Object
aliases for count
670 671 672 |
# File 'lib/mapplz.rb', line 670 def size(where_clause = nil, add_on = nil) count(where_clause, add_on) end |
#to_geojson ⇒ Object
152 153 154 155 |
# File 'lib/mapplz.rb', line 152 def to_geojson geojson = { type: 'FeatureCollection', features: query.map { |feature| JSON.parse(feature.to_geojson) } } geojson.to_json end |
#where(where_clause = nil, add_on = nil) ⇒ Object
aliases for query
665 666 667 |
# File 'lib/mapplz.rb', line 665 def where(where_clause = nil, add_on = nil) query(where_clause, add_on) end |