8

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!

JJD
  • 50,076
  • 60
  • 203
  • 339
Larry
  • 664
  • 6
  • 9

4 Answers4

6

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

steenhulthin
  • 4,553
  • 5
  • 33
  • 52
  • More specifically, you need to transform via Proj.4 http://virtuoso.rubyforge.org/rgeo/RGeo/CoordSys/Proj4.html – Mike T Aug 07 '11 at 11:07
  • Thank you so much, and I've been trying to test this out for a full day now. I can't actually test it yet because now I've run into this: https://github.com/dazuma/rgeo/issues/6 Hopefully, I can work around that and then accept your answer. – Larry Aug 08 '11 at 16:08
1

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

Community
  • 1
  • 1
Larry
  • 664
  • 6
  • 9
0

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
Steve Lorek
  • 111
  • 1
  • 1
  • 7
0

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

flexus
  • 465
  • 3
  • 9
  • I am trying with the same command above but getting an error of unable to open, i have only shape file does it need .shx and .dbf too. – Gourav Jul 03 '17 at 11:09