【问题标题】:How can I transform the coordinates of a Shapefile?如何转换 Shapefile 的坐标?
【发布时间】:2015-11-06 11:08:46
【问题描述】:

我正在尝试将社区数据导入我的应用程序,但我使用的数据有问题,我从 here 获得。

此文件包含一个具有旧金山社区的 shapefile。我正在运行一个 Ruby on Rails 框架,我目前正在使用 GeoRuby 来解析 shapefile。

代码如下所示:

def self.run_import
  shpfile = '/path/to/realtor_neighborhoods/realtor_neighborhoods'
  ShpFile.open(shpfile) do |shp|
    shp.each do |shape|
      # This gets the first (and only) Polygon from each MultiPolygon
      polygon = shape.geometry.geometries.first 
      puts polygon.inspect
    end
  end
end

代码能够解析文件,但我无法理解解释的坐标。所有点的值都以百万计,当我希望坐标在 -180 和 180 之间时,有效的纬度和经度。看一个例子点:

<GeoRuby::SimpleFeatures::Point:0x00000104566a08 @srid=4326, @with_z=false, \
   @with_m=false, @x=6015402.9999795845, @y=2114960.4999904726, @z=0.0, @m=0.0>,

这些坐标值的格式是什么?如何将它们转换为对我有意义的值? (即基于 SRID 4326 WGS84 空间参考系统的纬度/经度)

提前谢谢你!

【问题讨论】:

    标签: ruby-on-rails ruby gis postgis shapefile


    【解决方案1】:

    形状文件中的数据是projected geographic data

    从您的问题看来,您实际上只是更喜欢以纬度/经度为单位的数据。为此,您需要重新投影数据。我不是 ruby​​ 人,但快速的网络搜索显示 georuby 不支持重投影 http://georuby.rubyforge.org/,但 rgeo 支持。 http://www.daniel-azuma.com/blog/archives/28

    如果您想了解更多关于地图投影的信息,请查看here

    顺便说一下,有一个供 GIS(地理信息系统)专家使用的 stackexchange 站点,名为 http://gis.stackexchange.com

    【讨论】:

    【解决方案2】:

    我注意到这仍在获取视图日志。我最终在 RGeo 上苦苦挣扎,但还有另一种解决方案。如果您能够/愿意在执行 ruby​​ 代码之前/在外部进行转换,请查看 ogr2ogr。

    我在底部的评论中有更多详细信息: How Can I Use (Ruby) RGeo to Transform (Unproject) Coordinates

    【讨论】:

      【解决方案3】:

      我遇到了这个问题,因为我想将 Shapefile 中提供的点从 OSGB36 British National Grid 格式转换为 WGS84 格式(十进制度)。我花了很多时间弄清楚这一点,所以希望这段代码会很有用。

      此代码使用 ffi-ogr gem 并需要 GDAL 库:

      require 'ffi-ogr'
      
      data = OGR.read file_name
      new_spatial_ref = OGR.import_sr(4326, 'epsg')
      
      data.layers.each do |layer|
        transformer = OGR::CoordinateTransformation.find_transformation(layer.spatial_ref, new_spatial_ref)
      
        layer.features.each do |feature|
          geometry = OGR::Tools.cast_geometry(feature.geometry)
          geometry.transform(transformer)
      
          # Do something with geometry here
        end
      end
      

      【讨论】:

        【解决方案4】:

        我遇到了同样的问题:想将投影地理数据转换为纬度/经度值。

        ogr2ogr 工具比我预期的要好用得多。

        安装:

        apt-get install gdal-bin

        获取有关您的 shapefile 的信息:

        ogrinfo data.shp -al -so

        转换为经纬度和 JSON:

        ogr2ogr -f GeoJSON -t_srs WGS84 data.json data.shp

        【讨论】:

        • 我正在尝试使用上面相同的命令,但出现无法打开的错误,我只有形状文件,它也需要 .shx 和 .dbf。
        猜你喜欢
        • 2018-04-22
        • 2022-08-19
        • 1970-01-01
        • 1970-01-01
        • 2021-05-29
        • 1970-01-01
        • 2021-06-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多