[GIS] Converting UTM to Lat/Long with a geoJSON file in pyproj

coordinate systemepsggeojsonpyprojpython

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.

Best Answer

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, long_grid)

#write reprojected json to new file
with open('path_to_new_file.json', 'w') as f:
    f.write(json.dumps(data))

Hope this helps.