How can I transform the coordinates of a Shapefile?

天涯浪子 提交于 2019-12-06 22:45:02

问题


I am trying to get neighborhood data into my application, and I'm having problems with the data I am using, which I got from here.

This file contains a shapefile that has the neighborhoods of San Francisco. I am running a Ruby on Rails framework, and I'm currently using GeoRuby to parse the shapefile.

The code looks like this:

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

The code is able to parse the file, but I am unable to understand the coordinates as interpreted. All of the points have values in the millions, when I would expect coordinates between -180 and 180, for valid latitude and longitude. Take a look at an example point:

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

What is the format of these coordinate values? How can I convert them to values that are meaningful to me? (i.e. latitude/longitude based on the SRID 4326 <=> WGS84 spatial reference system)

Thank you in advance!


回答1:


The data you have from the shape file is projected geographic data.

From your question it sounds like you would really just prefer to have your data in lat/long. To get that you need to reproject your data. I am not a ruby guy, but a quick web search reveals that georuby does not support reprojection http://georuby.rubyforge.org/, however rgeo does. http://www.daniel-azuma.com/blog/archives/28

If you would like to know more about map projections have a look here.

By the way there is a stackexchange site for GIS (geographic information systems) experts called http://gis.stackexchange.com




回答2:


I noticed this is still getting a log of views. I ended up struggling with RGeo, but there's another solution. If you are able/willing to do your conversion outside/before you execute your ruby code, check out ogr2ogr.

There are more details in my comment on the bottom here: How Can I Use (Ruby) RGeo to Transform (Unproject) Coordinates




回答3:


I came across this question as I wanted to transform points supplied in a Shapefile from OSGB36 British National Grid format to WGS84 format (decimal degrees). I spent a lot of time figuring this out so hopefully this code will prove useful.

This code uses the ffi-ogr gem and requires the GDAL library:

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:


I had the same problem: wanted to convert projected geodata to Lat/Long values.

The ogr2ogr tool was much easier to use than I expected.

To install:

apt-get install gdal-bin

Get info about your shapefile:

ogrinfo data.shp -al -so

Convert to Lat/Long and JSON:

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



来源:https://stackoverflow.com/questions/6963991/how-can-i-transform-the-coordinates-of-a-shapefile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!