2

我在问是否有人已经完成了以下描述。我什至不知道这是否可能。

我想使用 KML 文件生成记录在我的 PostgreSQL 数据库(使用 PostGIS)上的多边形。

4

1 回答 1

3

I finally did it

geometry = GeoRuby::SimpleFeatures::MultiPolygon.new
doc = kml =~ /\<kml / ? Nokogiri::XML(kml) : Nokogiri::XML.fragment(kml)
doc.search('Polygon').each_with_index do |hpoly,i|
  poly = GeoRuby::SimpleFeatures::Geometry.from_kml(hpoly.to_s)
end
geometry.empty? ? nil : geometry

The kml file is directly the uploaded file where I applied the open method.

I've found a lot of inspiration coming from this document of inaturalist

By the way. I found another problem: store it. I didn't find any way to convert (and project) points coming from GeoRuby to RGeo. That's why I finally parse it by myself:

@doc = Nokogiri::XML(kml)
@doc.css('Placemark').each do |placemark| 
  coordinates = placemark.at_css('coordinates')

  if coordinates
    coordinates.text.split(' ').each do |coordinate|
      (lon,lat,elevation) = coordinate.split(',')
      points << Geo::StorageFactory.point(lon.to_f, lat.to_f)
      print "#{lat},#{lon}"
        puts "\n"
      end

  end
end

@area = Geo::StorageFactory.polygon(Geo::StorageFactory.line_string(points)).projection
于 2014-05-03T17:31:27.623 回答