如何做 geojson 与 rgeo 的交集?
How to do an intersection of geojson with rgeo?
我是 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)
您的代码的问题是 RGeo::GeoJSON.decode
不是 return 几何图形,而是 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
我是 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)
您的代码的问题是 RGeo::GeoJSON.decode
不是 return 几何图形,而是 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