Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I would like to convert the tree inventory shape files of London (Canada) into a GPX file and if possible into a JSON file. You can download the archive from their Open Data website. It contains the following files.

READ_ME.txt
TREEINV_SPECIES.xml
TREEINV_SPECIES.xsd
TREES.DBF
trees.gpx
trees.sbn
trees.sbx
trees.shp
trees.shp.xml
trees.shx

Today, I stumbled into ogr2ogr and tried the following command.

$ ogr2ogr -f GPX trees.gpx trees.shp

Though, the following error occurs.

ogr2ogr ERROR 1: Latitude is invalid. Valid range is [-90,90].  
This warning will not be issued any more

I already found a similar question but cannot figure out what the problem is. Please mind, I am totally new to the topic of converting geographic data. If you know any better tool I am willing to try it out.


EDIT: An initial success story.

According to the answers of Aragon and Brent Edwards I came up with the following commands. I found a webpage stating that data can be ordered in NAD83. So I assume that the dataset I am trying to convert comes as NAD83 as well. However, I cannot understand how I can find out the EPSG resp. SRID myself. With the given information I can lookup EPSG:26917 in the EPSG Geodetic Parameter Registry - but if I have no clue what the number is - how would I do that?

$ ogr2ogr -f GeoJSON -s_srs EPSG:26917 -t_srs EPSG:4326 trees_coordinates.json trees.shp
$ ogr2ogr -f GeoJSON -s_srs EPSG:26917 TREES_properties.json TREES.DBF

The converted GeoJSON files contain the following data.

// Example rows from trees_coordinates.json
{
"type": "FeatureCollection",
"features": [
{ "type": "Feature", "properties": { }, "geometry": { "type": "Point", "coordinates": [ -81.233494, 42.994146 ] } }
,
{ "type": "Feature", "properties": { }, "geometry": { "type": "Point", "coordinates": [ -81.232346, 42.994477 ] } }
,
{ "type": "Feature", "properties": { }, "geometry": { "type": "Point", "coordinates": [ -81.232176, 42.994527 ] } }
,
{ "type": "Feature", "properties": { }, "geometry": { "type": "Point", "coordinates": [ -81.245137, 42.971636 ] } }
,
{ "type": "Feature", "properties": { }, "geometry": { "type": "Point", "coordinates": [ -81.189495, 42.996498 ] } }
,
{ "type": "Feature", "properties": { }, "geometry": { "type": "Point", "coordinates": [ -81.189651, 42.996442 ] } }

]
}

And ..

// Example rows from TREES_properties.json
{
"type": "FeatureCollection",
"features": [
{ "type": "Feature", "properties": { "TREEID": 19.000000, "DIAMETER": 53.000000, "SPECIES": "MASU" }, "geometry": null }
,
{ "type": "Feature", "properties": { "TREEID": 23.000000, "DIAMETER": 30.000000, "SPECIES": "MANO" }, "geometry": null }
,
{ "type": "Feature", "properties": { "TREEID": 24.000000, "DIAMETER": 53.000000, "SPECIES": "MASI" }, "geometry": null }
,
{ "type": "Feature", "properties": { "TREEID": 104920.000000, "DIAMETER": 5.000000, "SPECIES": "OAWH" }, "geometry": null }
,
{ "type": "Feature", "properties": { "TREEID": 60358.000000, "DIAMETER": 34.000000, "SPECIES": "MAMA" }, "geometry": null }
,
{ "type": "Feature", "properties": { "TREEID": 60355.000000, "DIAMETER": 51.000000, "SPECIES": "MASU" }, "geometry": null }

]
}

Questions:

  1. Are my commands correct?
  2. How can I match the rows of both files? Which property set belongs to which GPS position? Is their a way to combine both in one file? I saw another format that includes all information in one file.
  3. How can I find out the EPSG/SRID and SRS in general if I have nothing more than those files listed in the beginning of my question?
share|improve this question
    
To determine the projection of the shapefile when the *.prj file is missing, refer to the following: gis.stackexchange.com/questions/4544/… gis.stackexchange.com/questions/9251/… Regarding SRID/spatial reference lookup - the following site is excellent - spatialreference.org For your issues with the GeoJSON conversion - you should be able to get the attributes and geometry with a single ogr2ogr conversion on the shapefile - like in your first command. –  Brent Edwards May 24 '12 at 12:54
    
Can you add an example command to "get the attributes and geometry with a single ogr2ogr converstion", please? I cannot figure out how to do it. –  JJD May 25 '12 at 1:26
    

4 Answers 4

If you can't be bothered with Esri's licensing, here is a free option using GDAL/Python:

from osgeo import ogr
ds_fname = r'C:\Temp\countries.shp' # update this to your Shapefile
ds = ogr.Open(ds_fname)
if not ds:
    raise IOError('Could not open ' + ds_fname)
layer = ds.GetLayer()
for fid in range(layer.GetFeatureCount()):
    feat = layer.GetFeature(fid)
    json = feat.ExportToJson()
    # Only print the first 70 characters of GeoJSON output, since it can be long
    print('%3i : %s ...'%(fid, json[:80]))

and if you only need the geometries and not the attributes:

    geom = feat.GetGeometryRef()
    json = geom.ExportToJson()

here is a bit of my output, showing the feature ID, then the first bit of the geoJSON output:

  0 : {"geometry": {"type": "Polygon", "coordinates": [[[-9.0430910000000004, 70.80386 ...
  1 : {"geometry": {"type": "MultiPolygon", "coordinates": [[[[-6.717778, 61.933326999 ...
  2 : {"geometry": {"type": "Polygon", "coordinates": [[[6.3621699999999999, 49.459389 ...
  3 : {"geometry": {"type": "MultiPolygon", "coordinates": [[[[7.2083649999999997, 53. ...
  4 : {"geometry": {"type": "Polygon", "coordinates": [[[9.5986349999999998, 47.063834 ...
  5 : {"geometry": {"type": "MultiPolygon", "coordinates": [[[[7.3916089999999999, 43. ...
...
share|improve this answer
up vote 8 down vote accepted

To answer my own question on how to combine both the .dbx (properties) and the .shp (geometries) into a single JSON file: The problem I did not see is that all file names must be lower case to enable ogr2ogr to do the conversion. That should not be neccessary if your file system is case-insensitive but mine is. With this requirements fulfilled ogr2ogr is able to associate the .shp and .dbx files with each other. - The command is the following.

ogr2ogr -f GeoJSON -s_srs EPSG:26917 -t_srs EPSG:4326 trees.json trees.shp

Here is another link that might be of interest in this context. This webpage allows to read the projection information from a .prj file.

share|improve this answer
1  
Also, you could use only the -t_srs flag; but you can't only use the -s_srs flag, like in the example in your question. –  bozdoz Oct 27 at 19:08

i think there is a projection problem in this file . make a new file under the same dir as name of trees.prj and then past the following definition in it. then run your ogr2ogr command...

PROJCS["NAD_1983_UTM_Zone_17N",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["Transverse_Mercator"],PARAMETER["False_Easting",500000.0],PARAMETER["False_Northing",0.0],PARAMETER["Central_Meridian",-81.0],PARAMETER["Scale_Factor",0.9996],PARAMETER["Latitude_Of_Origin",0.0],UNIT["Meter",1.0]]

you can also use the MyGeodata Converter ... after adding below definition it automatically define srid as SRID:26917 and projection NAD_1983_UTM_Zone_17N...

MyGeodata Converter has GPX and GeoJSON :

List of available output data formats

ESRI Shapefile
Microstation DGN
MapInfo File
Comma Separated Value (.csv)
GML
GPX
KML
GeoJSON

i hope it helps you...

share|improve this answer
    
super helpful, practical response –  ako May 22 '12 at 19:49

The GPX format assumes WGS84 datum (EPSG:4326). The shapefile in question has no defined projection (*.prj file). The ogr2ogr command thus assumes that your file is already in WGS84 and then calls an error when it comes across coordinates outside the valid range for WGS84.

To solve this problem, you need to find out what projection the shapefile is in and then use the "-s_srs switch to define the projection of your input shapefile in your ogr2ogr command to do the GPX conversion.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.