Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Some geospatial ruby libraries

366 views

Published on

Some geospatial ruby libraries lightning talk presented at Leeds Ruby Thing, April 2016

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Some geospatial ruby libraries

  1. 1. Some Ruby Geospatial Libraries
  2. 2. Rosemary require 'rosemary' client = Rosemary::BasicAuthClient.new  ('osm_user_name', 'password') api = Rosemary::Api.new(client) changeset = api.create_changeset("Add  postbox") node = Rosemary::Node.new(lat, lon) node.add_tags({"amenity"=>"postbox"}) api.save(node, changeset) api.close_changeset(changeset)
  3. 3. Overpass API
  4. 4. require 'overpass_api_ruby' pub_query = "<union> <query type='relation'> <has­kv k="amenity" v="pub"/> </query> </union>" Options ={:bbox => {:s =>­34.7,:n => ­34.2, :w => ­58.53, :e => ­58.33},          :timeout => 900,          :element_limit => 1073741824,          :json => true} overpass = OverpassAPI.new(options) result_hash = overpass.query(pub_query)
  5. 5. GEOS def buffer(feature, radius, min_radius=0.0)    wkb_writer = Geos::WkbWriter.new    wkb_reader = Geos::WkbReader.new    geom =  wkb_reader.read_hex(feature.geometry)    buffer_geom = geom.buffer(radius)    if min_radius != 0.0       hole_geom = geom.buffer(min_radius)       buffer_geom = buffer_geom.difference(hole_geom)    end    feature.geometry = wkb_writer.write_hex buffer_geom    feature end
  6. 6. GDAL / OGR ● https://github.com/zhm/gdal-ruby Gdal::Ogr.create_geometry_from_wkt('POINT (30 10)') raster = Gdal::Gdal.open(filename) dx = raster.RasterXSize dy = raster.RasterYSize x0, x_res, x_skew, y0, y_skew, y_res = raster.get_geo_transform
  7. 7. RGeo https://github.com/rgeo/rgeo factory = Rgeo::Cartesian.factory point1 = factory.point(1, 0) point2 = factory.point(1, 4) point3 = factory.point(­2, 0) point2.distance(point3) # => 5.0 line_string1 = factory.line_string([point1, point2, point3]) line_string1.num_points # => 3 line_string1.point_n(0) == point1 # => true line_string1.end_point == point3 # => true
  8. 8. PostGIS / postgres https://github.com/rgeo/activerecord-postgis-adapter create_table :my_spatial_table do |t|   t.column :shape1, :geometry   t.geometry :shape2   t.line_string :path, srid: 3785   t.st_point :lonlat, geographic: true   t.st_point :lonlatheight, geographic: true, has_z: true end
  9. 9. PostGIS / postgres  bbox_polygon =  Polygon.from_coordinates([bbox_coords], 4326).as_ewkt Model.where(ST_Within(bbox_geom,                       ST_GeomFromText('#{bbox_polygon}'))")
  10. 10. map = Mapnik::Map.new do |m|   m.background = Mapnik::Color.new('#777')  # A grey background   # Use the Google mercator projection   m.srs =  Mapnik::Tile::DEFAULT_OUTPUT_PROJECTION   # Add a layer to the map   m.layer 'countries' do |l|     l.style do |s|       # Add a rule to the style (this one is a default rule)       s.rule do |default|         default.fill = Mapnik::Color.new('#880000')       end     end     l.srs = "+proj=latlong +datum=WGS84" # set the srs of the layer     l.datasource = Mapnik::Datasource.create :type => 'shape',   :file => "myshapefile.shp"   end end map.zoom_to_box(map.layers.first.envelope)  map.render_to_file('my_map.png') Ruby Mapnik

×