go>> rwc>> 返回
项目作者: ash106

项目描述 :
Water rights management system for Riley Water Consulting
高级语言: Ruby
项目地址: git://github.com/ash106/rwc.git
创建时间: 2014-06-26T04:42:34Z
项目社区:https://github.com/ash106/rwc

开源协议:

下载


Riley Water Consulting

Build Status
Coverage Status
Code Climate

Riley Water Consulting

Demo

Built With

  • Ruby on Rails - Used for parsing KML files, GeoJSON API endpoint, etc.
  • Bootstrap - CSS framework used for site design
  • Google Maps API - Used to draw points and polygons based on GeoJSON data from Rails

Relevant Code

  1. # app/services/kml_parser.rb
  2. class KmlParser
  3. def initialize(id, model_name)
  4. @id = id
  5. @model_name = model_name.constantize
  6. end
  7. def parse_polygon
  8. model = get_model
  9. doc = open_doc(model)
  10. coordinates_array = parse_coordinates(doc.at_css("polygon coordinates").text)
  11. polygon =
  12. {
  13. type: "Polygon",
  14. coordinates: [
  15. coordinates_array
  16. ]
  17. }
  18. model.update_column(:polygon, polygon)
  19. end
  20. def parse_point
  21. model = get_model
  22. doc = open_doc(model)
  23. coordinates_set = parse_coordinates(doc.at_css("point coordinates").text)
  24. point =
  25. {
  26. type: "Point",
  27. coordinates: coordinates_set[0]
  28. }
  29. model.update_column(:point, point)
  30. end
  31. private
  32. def get_model
  33. @model_name.find(@id)
  34. end
  35. def open_doc(model)
  36. Nokogiri::HTML(open(model.kml.url))
  37. end
  38. def parse_coordinates(coordinates)
  39. coordinates.scan(/(-?\d+.\d+),(-?\d+.\d+)/).collect { |lon, lat| [lon.to_f, lat.to_f]}
  40. end
  41. end
  1. # app/helpers/application_helper.rb
  2. def static_map(geometry)
  3. # If polygon, encode polygon and attach to URL
  4. if geometry["type"] == "Polygon"
  5. encoded_poly = encode_polygon(geometry)
  6. image_tag URI.encode("https://maps.googleapis.com/maps/api/staticmap?key=#{google_maps_api_key}&size=200x200&path=fillcolor:0x00000077|color:0x000000FF|weight:2|enc:#{encoded_poly}")
  7. # If point, just attach point in lat,lon format
  8. elsif geometry["type"] == "Point"
  9. point = "#{geometry["coordinates"][1]},#{geometry["coordinates"][0]}"
  10. image_tag URI.encode("https://maps.googleapis.com/maps/api/staticmap?key=#{google_maps_api_key}&size=200x200&zoom=14&markers=#{point}")
  11. end
  12. end
  13. def encode_polygon(poly)
  14. coordinates = []
  15. # If more than 200 points in polygon, then simplify polygon
  16. if poly["coordinates"][0].length > 200
  17. simple_coordinates = simplify_coordinates(poly["coordinates"][0])
  18. # Parse coordinates into expected format for encode method
  19. simple_coordinates.each do |c|
  20. coordinates << [c[:y], c[:x]]
  21. end
  22. # If less than 200 points in polygon, just parse into expected format for encode method
  23. else
  24. poly["coordinates"][0].each do |c|
  25. coordinates << [c[1], c[0]]
  26. end
  27. end
  28. # Encode points using Google encoded polyline algorithm
  29. Polylines::Encoder.encode_points(coordinates)
  30. end
  31. def simplify_coordinates(coordinates)
  32. # Parse coordinates into expected format for simplify method
  33. coordinates_hash = []
  34. coordinates.each do |c|
  35. coordinates_hash << {x: c[0], y: c[1]}
  36. end
  37. # Simplify coordinates using tolerance: 0.001
  38. SimplifyRb.simplify(coordinates_hash, 0.001)
  39. end

Getting Started

Prerequisites

  1. ruby -v # 2.4.1
  2. rails -v # 5.1.3

Installing

Install dependencies

  1. bundle install

Create .env file in project’s root directory (to easily get a SECRET_KEY_BASE run rake secret)

  1. RACK_ENV=development
  2. SECRET_KEY_BASE=secret-key-base
  3. S3_BUCKET_NAME=s3-bucket-name
  4. AWS_ACCESS_KEY_ID=aws-id
  5. AWS_SECRET_ACCESS_KEY=aws-secret-key
  6. GOOGLE_MAPS_KEY=google-maps-key
  7. SENDGRID_USERNAME=sendgrid-username
  8. SENDGRID_PASSWORD=sendgrid-password

Setup database

  1. rake db:setup

Run server

  1. foreman start

Click a point or polygon on the map