0

我是 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)

4

1 回答 1

2

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

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

这个片段做你想要的:

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
于 2019-12-28T09:39:11.567 回答