How can I transform the coordinates of a Shapefile? - ruby-on-rails

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!

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

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

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

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

Related

Moving to OpenLayers from Google maps

I'm looking for some help on comparative OpenLayer functions for the following Google maps functions, can someone please let me know what these would be?
I'm currently using
For getting distance, using the distance matrix API:
http://maps.googleapis.com/maps/api/distancematrix
For getting latitude and longitude of the current address:
http://maps.googleapis.com/maps/api/geocode/json?address
Kindly check the attachment which are using for getting latitude, longitude and distance matrix.
Function names:
function getLatLng($add)
function getRoadDistance($from, $to)
--
things are a little bit more complicated in OL than they are with the google-api
Routing: if you have a small road network you can consider creating a Database in PostGIS and use pgrouting to get routing functions, you can find more on it here
Or if you want to use it on a bigger scale there some APIs that do it for you, for example yourNavigator, you'll have to make a get request with your coordinates like this
http://www.yournavigation.org/api/1.0/gosmore.php?format=geojson&flat=startLatitude&flon=startLongitude&tlat=destLatitude&tlon=destinationLongitude
there is also the OSRM for the same purpose.
to get the longitude and latitude based on an address you can use the geocoder for Openlayers 3
that's what I could think of right now, I hope it helps

What kind of coordinates are these numbers?

I have geo data, that contain X field like: 1012532,749 and Y field like: 178774,7655. This data from the shapefile format, but I don`t know in what GEO standart this data is.
Maybe someone know, or can show me the way to find out, how translate this coords in GPS.
From your comment the situation is clear:
You got an invalid shape file:
The prj file states WGS84 coordinates in decimal degrees
which has range: (longitude(x) [-180-.0, 180.0], latitude(y): [-90.0, 90.0].
But the coordinates posted are not in valid range.
The prj definition does not fit to the rest of the shape files (your posted coordinate).
(This happens because the prj file is optional, and has probably the default settings of some other project)
There is little chance of knowing, without further knowlegde.
Simply ask the data provider which geographical datum (name) the coordinates are related to. Further you should claim, that the file is erorrnous and that they should provide a correct prj file (or remove the prj file, if the coordinates are not related to world coordinates)
IF you know the source, e.g the swiss "Landesvermessung" then you could think it is for example a "Swiss Grid CH1903+ / LV95" grid system. (This was an example, the coordinates are not in that Swiss grid)
But it does not make sense to reverse engineer that, just ask the data provider, or if appliable read the info where you have got that data from.

Converting from Albers to GPS coordinates

I have some coordinate data and the only thing i know about its coordinate system is the
following description:
PROJCS["Basic Albers NAD83",
GEOGCS["GCS_North_American_1983",
DATUM["D_North_American_1983",
SPHEROID["GRS_1980",6378137.0,298.257222101]
],
PRIMEM["Greenwich",0.0],
UNIT["Degree",0.0174532925199433]
],
PROJECTION["Albers"],
PARAMETER["False_Easting",0.0],
PARAMETER["False_Northing",0.0],
PARAMETER["Central_Meridian",-96.0],
PARAMETER["Standard_Parallel_1",45.5],
PARAMETER["Standard_Parallel_2",29.5],
PARAMETER["Latitude_Of_Origin",23.0],
UNIT["Foot_US",0.3048006096012192]
]
this comes from a prj arcGis file.
I need to convert from this system to GPS (Google maps) system and vice-versa.
this must be done programatically and i cannot use esri libraries (license issues)...
Could you explain the meaning of all these parameters?
are the coordinates expressed in the spheroid and the projection (albers) used only when drawing
the map? or the coordinates depend on the projection used also?
Do you know an open source library I could use for this transformation?
Thanks!
All these parameters define the projection, including:
Projection type (Albers)
GEOGCS defines the geoid (idealized Earth shape) used by the project. In this case, it's NAD83, which uses degrees for units and is centered at the Greenwich, the 0th longitude.
Where the projection itself is centered
How far the projection is moved north and east to make any negative coordinates
positive (False Easting and False Northing)
Units for coordinates in this projection (US Feet)
Some other information about how coordinates of the projection were transformed to be drawn on a flat service.
I have a blog post which goes over the details for an OGC WKT definition, which is very similar to the content in an ESRI .prj file: http://www.dev-garden.org/2011/07/30/projections-for-programmers-one-projection/. You may find it useful.
With projected coordinates, they have already been 'drawn on the map', and you need to reproject them to use in other coordinate systems like Google's projection.
There are many open source tools you can use to reproject your coordinates from one projected system to another. Here are a few:
Geotools or Proj4j - Java
Proj4js - Javascript
Proj - C or C++
dotspatial - C#
One command line tool I find very useful is ogr2ogr. Using ogr2ogr, it is very easy to reproject coordinates from one system to another. Here's how to convert a projected shapefile to a Google Earth projection:
ogr2ogr -f "ESRI Shapefile" -t_srs EPSG:3857 NEWFILE.shp OLDFILE.shp
Ogr2ogr uses the .prj file to determine the current projection of the shapefile, and then reprojects it to Googles projection, defined here by the EPSG shorthand EPSG:3857. You can find ogr2ogr for Windows and other platforms at http://fwtools.maptools.org/

GDAL library orginfo -spat option

Has anyone used the ogrinfo [-spat xmin ymin xmax ymax] option in the GDAL tools? I am able to run the - sql query on the shape files and get the answers/shapes but however if I use the lat/long values in spat, I dont get the results (However I dont get an error). I could not find an example at all.
This suggests one of three options:
Your file is not georeferenced as you expect
Your area of interest extent values are entered incorrectly
There are no features that both match your sql query AND lie within the extent as you have defined it.
You can test your understanding of the -spat values by creating a polygon to match and overlaying it in (say) QGIS to see how it overlaps with your data. This will quickly help you eliminate the options above. After that - you'll understand the feature for future use :)

British National Grid Shapefile - convert to WGS84 Lat/Lon

I have a set of ESRI shapefiles which, I'm told, have been written using the British National Grid coordinate system. I need to convert these files to WGS84 lat/lons, for onward conversion to KML files. I'm having trouble doing this as follows:
If I open each of the original files in MapInfo Professional telling it that my file has a projection which is British National Grid then I can't see any geographic objects in the file; the map window is completely empty.
If I use MapInfo Professional's Universal Translator to convert the files to a WGS84 MapInfo TAB file then, just as before, the converted file won't display any geographic objects, the MapInfo window is empty.
Can I verify the coordinate system of these files? Am I missing anything here? Should I be able to convert the shapefiles in the way I'm expecting to be able to and view them using MapInfo Professional? Will another tool do a better job for me?
Thanks.
More Info:
My shapefile has coordinates which don't seem to translate to lat/lon properly and I'm now wondering if the coordinates aren't actually British National Grid. I'm seeing coordinates such as 383702523, 399081141 which appears to be approximately lat/lon 53.488182, -2.247153. Have you any idea what projection system my input file is in?
OS grid doesn't use WGS84 - it uses Airy 1936 (OSGB36) spheroid
So you need to go from OSgrid -> lat/lon then OSGB36->WGS84.
See http://www.ordnancesurvey.co.uk/oswebsite/gps/docs/convertingcoordinatesEN.pdf
To do OS grid to lat-lon see http://www.movable-type.co.uk/scripts/latlong-gridref.html
Then to go from OSGB36 -> WGS84 see http://www.movable-type.co.uk/scripts/latlong-convert-coords.html
http://gothos.info/2009/04/14/transform-projections-with-gdal-ogr/
ogr2ogr is a great tool for doing these sorts of conversions. You would run it with a command like
ogr2ogr -t_srs EPSG:4326 map_wgs84.shp map_original.shp
-t_srs is the option to transform co-ordinate systems. 4326 is the EPSG SRID for WGS84.

Resources