【问题标题】:How to find all polygons thats includes special lat long point如何查找包含特殊纬度长点的所有多边形
【发布时间】:2017-07-18 11:46:38
【问题描述】:

我使用 rgeo 和 activerecord-postgis-adapter gem。我想查找多边形中包含特殊点的所有记录。我在谷歌地图上标记了矩形,并期望如果点在里面,sql 返回行,当点在外面时不返回行。不幸的是point_outside_2 也返回结果。我做错了什么?我应该使用投影而不是真正的经纬度吗?

describe 'polygon' do
  let(:factory) { RGeo::Geographic.simple_mercator_factory }

  let(:left_up_corner) { factory.point(50.095073, 19.852121) }
  let(:right_up_corner) { factory.point(50.092230, 20.057740) }
  let(:left_bottom_corner) { factory.point(50.021297, 19.857577) }
  let(:right_bottom_corner) { factory.point(50.015820, 20.051943) }

  let(:point_inside_1) { factory.point(50.059631, 19.939323) }
  let(:point_inside_2) { factory.point(50.029995, 19.941997) }
  let(:point_outside_1) { factory.point(50.153008, 19.990906) }
  let(:point_outside_2) { factory.point(50.118037, 19.970446) }


  let(:line) { factory.line_string([left_up_corner, right_up_corner, right_bottom_corner, left_bottom_corner]) }
  let(:area) { factory.polygon(line) }

  it 'finds nurses with polygon include point' do
    Nurse.create(area: area)
    expect(count_nurses(point_inside_1)).to be 1
    expect(count_nurses(point_inside_2)).to be 1
    expect(count_nurses(point_outside_1)).to be 0
    expect(count_nurses(point_outside_2)).to be 0 # it return 1
  end

  def count_nurses(point)
    Nurse.where("ST_DWithin(area, ST_Point(#{point.coordinates.join(',')}), 4326)").count
  end
end

迁移:

class AddPolygonToNurse < ActiveRecord::Migration[5.1]
  def change
    add_column :nurses, :area, :st_polygon, :geographic => true
    add_column :nurses, :latlong, :st_point, :geographic => true
  end
end

【问题讨论】:

  • ST_Dwithin 中,您要求在多边形 4326 米范围内的点。如果您在多边形内寻找点,它应该是 0。此外,您正在使用带有 simple_mercator_factory 的地理坐标(纬度/经度),这是一个投影坐标系(其中一个是错误的,无论是您的坐标还是您的工厂)
  • 是的,我相信他正在寻找ST_Contains 而不是ST_Dwithin。 4326 是 WGS86 地理投影系统的 ID。点 2 可能在框的 4.326 公里范围内。
  • @JGH 这是一个正确的答案。非常感谢。我将 4326 更改为 0,现在它可以工作了。
  • @AaronBreckenridge 你也有权利我想把 id 放在那里。我也尝试过使用 ST_contains,但还是有语法错误。

标签: ruby-on-rails postgis rgeo


【解决方案1】:

试试这个,我正在使用 RGeo 的应用程序中做类似的事情来查找多边形内的点。希望这对你有用

describe 'polygon' do
  let(:factory) { RGeo::Geographic.simple_mercator_factory }
  let(:ewkb_generator) do
    RGeo::WKRep::WKBGenerator.new(
      type_format:    :ewkb,
      emit_ewkb_srid: true,
      hex_format:     true
    )
  end

  let(:left_up_corner) { factory.point(50.095073, 19.852121) }
  let(:right_up_corner) { factory.point(50.092230, 20.057740) }
  let(:left_bottom_corner) { factory.point(50.021297, 19.857577) }
  let(:right_bottom_corner) { factory.point(50.015820, 20.051943) }

  let(:point_inside_1) { factory.point(50.059631, 19.939323) }
  let(:point_inside_2) { factory.point(50.029995, 19.941997) }
  let(:point_outside_1) { factory.point(50.153008, 19.990906) }
  let(:point_outside_2) { factory.point(50.118037, 19.970446) }


  let(:line) { factory.line_string([left_up_corner, right_up_corner, right_bottom_corner, left_bottom_corner]) }
  let(:area) { factory.polygon(line) }

  it 'finds nurses with polygon include point' do
    Nurse.create(area: area)
    expect(count_nurses(point_inside_1)).to be 1
    expect(count_nurses(point_inside_2)).to be 1
    expect(count_nurses(point_outside_1)).to be 0
    expect(count_nurses(point_outside_2)).to be 0 # it return 1
  end

  def count_nurses(point)
    ewkb = ewkb_generator.generate(point.projection)
    Nurse.where('ST_Intersects(area, ST_GeomFromEWKB(E?))', "\\\\x#{ewkb}").count
  end
end

【讨论】:

  • 这很有帮助,但不是直接的答案。 @JGH 解决了我的问题。您的解决方案基于不同的点格式。即使它是正确的,我认为它有点复杂
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-04-23
  • 2014-09-06
  • 2014-10-09
  • 1970-01-01
  • 1970-01-01
  • 2016-09-07
  • 1970-01-01
相关资源
最近更新 更多