【问题标题】:How to do an intersection of geojson with rgeo?如何做geojson与rgeo的交集?
【发布时间】:2020-04-17 20:15:45
【问题描述】:

我是 ruby​​ 新手,正在尝试 rgeo。

我有 2 个 geojson 文件: points.geojson 包含许多点 这是points.geojson的要点

outline.geojson 包含一个多边形 这是我的outline.geojson的要点:

我想要交点(轮廓中包含的点)

这是我正在尝试的...

require 'rgeo'
require 'rgeo/geo_json'

points_str = File.read("points.geojson")
points = RGeo::GeoJSON.decode(points_str, json_parser: :json)
puts points

outline_str = File.read("outline.geojson")
outline = RGeo::GeoJSON.decode(outline_str, json_parser: :json)
puts outline

puts points.intersection outline

我得到的错误: intersection.rb:12:in ': undefined method intersection' for #RGeo::GeoJSON::FeatureCollection:0x294ac44 (NoMethodError)

【问题讨论】:

    标签: ruby geojson intersection rgeo


    【解决方案1】:

    您的代码的问题是RGeo::GeoJSON.decode 不返回几何图形,而是RGeo::GeoJSON::FeatureCollection 的实例。

    您首先必须分别提取相关的几何图形,即点数组和多边形。

    这个 sn-p 做你想做的事:

    require 'rgeo'
    require 'rgeo/geo_json'
    
    points = RGeo::GeoJSON
      .decode(File.open('points.geojson'))
      .map(&:geometry)
    puts points
    
    polygon = RGeo::GeoJSON
      .decode(File.open("outline.geojson"))
      .first
      .geometry
    puts polygon
    
    contained_points = points.select { |p| p.within?(polygon) }
    puts contained_points
    

    【讨论】:

    • 这正是我想要的。
    猜你喜欢
    • 1970-01-01
    • 2021-08-01
    • 2021-12-18
    • 2018-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-12
    相关资源
    最近更新 更多