1

I want to convert this geojson file below into a shapefile using ogr2ogr command ogr2ogr output.shp input.geojson.

However, an offset is appearing in the output coordinates that make the shapefile not correctly aligned with the input.

input.geojson:

{
   "type":"FeatureCollection",
   "features":[
      {
         "type":"Feature",
         "geometry":{
            "type":"LineString",
            "coordinates":[[-7551132, 6293816],[-7560916, 6260489]],
         },
         "properties":{
            "id":36
         }
      }
   ],
   "crs":{
      "type":"name",
      "properties":{
         "name":"urn:ogc:def:crs:EPSG:3857"
      }
   }
}

Is there a problem with my geojson? What could be the cause of the offset?

  • How do you observe the offset? – user30184 Sep 16 '15 at 13:17
  • In QGIS - but I have found the origin of the problem see edit soon – Below the Radar Sep 16 '15 at 13:18
  • GeoJSON specification is here geojson.org/geojson-spec.html. The crs member should be on the top-level GeoJSON object in a hierarchy (in feature collection, feature, geometry order) and should not be repeated or overridden on children or grandchildren of the object. I can't say really if first GeoJSON is invalid. Does missing crs on feature collection level mean that it is the default CRS84 and it must not be overridden later? – user30184 Sep 16 '15 at 13:38
  • A new test by moving crs from Feature collection into Feature made no difference for GDAL. It stil interprets that data are in EPSG:3857. – user30184 Sep 16 '15 at 13:49
1

I made a shapefile from your linestring

ogrinfo line.shp -al
INFO: Open of `line.shp'
      using driver `ESRI Shapefile' successful.

Layer name: line
Geometry: Line String
Feature Count: 1
Extent: (-7560916.000000, 6260489.000000) - (-7551132.000000, 6293816.000000)
Layer SRS WKT:
(unknown)
OGRFeature(line):0
  LINESTRING (-7551132 6293816,-7560916 6260489)

Next I converted it into GeoJSON

ogr2ogr -f GeoJSON -a_srs epsg:3857 line.json line.shp

I can't find any difference with ogrinfo

ogrinfo -ro -al line.json
INFO: Open of `line.json'
      using driver `GeoJSON' successful.

Layer name: OGRGeoJSON
Geometry: Line String
Feature Count: 1
Extent: (-7560916.000000, 6260489.000000) - (-7551132.000000, 6293816.000000)
Layer SRS WKT:
PROJCS["WGS 84 / Pseudo-Mercator",
    GEOGCS["WGS 84",
        DATUM["WGS_1984",
            SPHEROID["WGS 84",6378137,298.257223563,
                AUTHORITY["EPSG","7030"]],
            AUTHORITY["EPSG","6326"]],
        PRIMEM["Greenwich",0,
            AUTHORITY["EPSG","8901"]],
        UNIT["degree",0.0174532925199433,
            AUTHORITY["EPSG","9122"]],
        AUTHORITY["EPSG","4326"]],
    PROJECTION["Mercator_1SP"],
    PARAMETER["central_meridian",0],
    PARAMETER["scale_factor",1],
    PARAMETER["false_easting",0],
    PARAMETER["false_northing",0],
    UNIT["metre",1,
        AUTHORITY["EPSG","9001"]],
    AXIS["X",EAST],
    AXIS["Y",NORTH],
    EXTENSION["PROJ4","+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +
x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext  +no_defs"],
    AUTHORITY["EPSG","3857"]]
OGRFeature(OGRGeoJSON):0
  LINESTRING (-7551132 6293816,-7560916 6260489)

For verification convert the GeoJSON back to shape:

ogr2ogr -f "ESRI Shapefile" roundtrip.shp line.json

What we got:

ogrinfo -al roundtrip.shp
INFO: Open of `roundtrip.shp'
      using driver `ESRI Shapefile' successful.

Layer name: roundtrip
Geometry: Line String
Feature Count: 1
Extent: (-7560916.000000, 6260489.000000) - (-7551132.000000, 6293816.000000)
Layer SRS WKT:
PROJCS["WGS_84_Pseudo_Mercator",
    GEOGCS["GCS_WGS_1984",
        DATUM["WGS_1984",
            SPHEROID["WGS_84",6378137,298.257223563]],
        PRIMEM["Greenwich",0],
        UNIT["Degree",0.017453292519943295]],
    PROJECTION["Mercator_1SP"],
    PARAMETER["central_meridian",0],
    PARAMETER["false_easting",0],
    PARAMETER["false_northing",0],
    UNIT["Meter",1],
    PARAMETER["latitude_of_origin",0.0]]
FID: Integer64 (11.0)
OGRFeature(roundtrip):0
  FID (Integer64) = 0
  LINESTRING (-7551132 6293816,-7560916 6260489)

Coordinates seem to be identical with the original ones.

Conclusion: Use low level tools for debugging. You can never know what a big program like QGIS is doing on the background.

  • the problem is converting geojson to shapefile, not shapefile to geojson – Below the Radar Sep 16 '15 at 13:25
  • Ok, I continued with going back from GeoJSON into shapefile. Looks still OK. – user30184 Sep 16 '15 at 13:32
  • Thank you for the verification. I think my problem is completely elsewhere – Below the Radar Sep 16 '15 at 13:40
  • I edited the GeoJSON that was created by GDAL and moved the crs to features. Ogrinfo reports the crs as EPSG:3857 and a new roundtrip to shapefile gives the same result as earlier. – user30184 Sep 16 '15 at 13:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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