5

I am trying to convert a geoJSON file in UTM (EPSG 29902) to WGS84. I've seen other examples online which show how to use the transform function in pyproj; however all the ones I have seen are for a single set of coordinates which are typed into the console. For example:

wgs84 = Proj(init = 'epsg:4326')
IrishGrid = Proj(init = 'epsg:29902')
x1,y1 = 318909.45839999989, 251792.64200000092
x2,y2 = transform(IrishGrid,wgs84,x1,y1)
print (x2,y2)

Could someone please tell me how I would convert all of the coordinates in my GeoJSON file from UTM to lat/long? I have been able to read in the json file but am unsure of how to transform every coordinate. I would also like to be able to create a new GeoJSON file with the projected coordinates.

Here is my code so far:

from pyproj import Proj, transform

import json

with open('C:\Program Files (x86)\Anaconda3 (64-bit)\some_file.json') as f:
    data = json.load(f)

for feature in data['features']:

presume I use the transform function within my for loop but am unsure of the proper form.

The format of my json file is

"type": "FeatureCollection", "features": [ { "type": "Feature", "properties": { "REGION": "Southern Region", "REG_CODE": "03", "DIVISION": "Cork West", "DIV_CODE": "0319", "DISTRICT": "Bandon", "DIST_CODE": "4300A", "SUB_DIST": "Kinsale", "SUB_IRISH": "Cionn tSáile", "SUB_CODE": "4305B", "COUNTY_1": "Cork", "COUNTY_2": null, "GEOGID": "M4305B", "Male2011": 5765, "Female2011": 5963, "Total2011": 11728, "PPOcc2011": 4054, "Unocc2011": 1177, "Vacant2011": 1013, "HS2011": 5231, "PCVac2011": 19.4, "CREATEDBY": "Paul Creaner" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 154039.34449999966, 50090.991299999878 ], [ 154039.8311, 50105.332900000736 ], [ 154041.9757000003, 50130.335699999705 ],

followed by a list of coordinates to form a polygon for about 500 regions.

2
  • Please edit your question to correct the indentation of the code. Also, does your file name really have multiple spaces in the install path? AND you're missing the r designator to trap the escape characters correctly (e.g., open(r'C:\...'))
    – Vince
    Nov 8, 2016 at 12:36
  • @Vince I have just edited my post for your comments (there were no multiple spaces in my install path - it just copied in weirdly). I will add the r designator. Could you give any info on how I could convert the coordinates of the entire json file? Thanks for your help.
    – Maeve90
    Nov 8, 2016 at 12:46

1 Answer 1

5

This should do the trick. I haven't been able to install the pyproj module, but at the point marked #do transformation. I tested it by doing simple arithmetic. I put in the function based on your code. Basic structure is:

  • Load data from file.
  • Traverse json string - essentially nested dictionaries and lists
  • Iterate through the coordinates. The coordinate lists are heavily nested to allow for multipart polygons
  • Write altered data to a new file

Code:

import pyproj
import json

#define projections
wgs84 = Proj('epsg:4326')
IrishGrid = Proj('epsg:29902')

#load in data
with open(r'path_to_file_you_want_to_edit.json', 'r') as f:
    data = json.load(f)

#traverse data in json string
for feature in data['features']:
    #print feature['geometry']['type']
    #print feature['geometry']['coordinates']


    #all coordinates
    coords = feature['geometry']['coordinates']

    #coordList is for each individual polygon
    for coordList in coords:
    
        #each point in list
        for coordPair in coordList:
            print coordPair
            x1 = coordPair[0]
            y1 = coordPair[1]
            lat_grid, lon_grid = numpy.meshgrid(x1, y1)
            #do transformation
            coordPair[0],coordPair[1] = pyproj.transform(IrishGrid,wgs84,lat_grid, lon_grid)

#write reprojected json to new file
with open('path_to_new_file.json', 'w') as f:
    f.write(json.dumps(data))
4
  • thanks a lot for the comment. I have tried this code; however I am getting this error: "RuntimeError: x,y and z must be same size" occurring at the transformation line. I have checked the geometry types and I have mostly polygons but also a few multipolygons - could this have something to do with it?
    – Maeve90
    Nov 8, 2016 at 16:30
  • @Maeve90, did it work?
    – RoperMaps
    Nov 8, 2016 at 16:32
  • I actually got it to work from adding the following line of code within the for coordPair loop: at_grid, lon_grid = numpy.meshgrid(x1, y1). Thanks so much for your help.
    – Maeve90
    Nov 8, 2016 at 17:44
  • Great, why not suggest an edit to my answer so it provides a working exempts for others. It can then be marked complete
    – RoperMaps
    Nov 8, 2016 at 17:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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