简体   繁体   中英

GeoPandas .to_crs() won't work as part of a function but does in a standalone program

I'm trying to reproject a single GeoJSON layer of points from EPSG:4326 to EPSG:3857. When I run the below code by itself in an isolated program (as below), the output is as expected.

geojsonSaveDir = 'path_to_input_file'
firePoints4326 = gpd.read_file(geojsonSaveDir)
firePointsReproject = firePoints4326.copy()
firePointsReproject['geometry'] = firePointsReproject['geometry'].to_crs(3857)
print(firePoints4326['geometry'].head())
print(firePointsReproject['geometry'].head())
firePointsReproject.crs = from_epsg(3857)
reprojectSaveDir = geojsonSaveDir.replace('.geojson', '') + '_3857'
firePointsReproject.to_file(reprojectSaveDir)

The output of the above code is:

0    POINT (-61.56000 -11.96000)
1    POINT (-61.58000 -11.96000)
2    POINT (-59.21000 -11.66000)
3    POINT (-59.23000 -11.66000)
4    POINT (-51.55000 -11.28000)
Name: geometry, dtype: geometry
1    POINT (-6855054.243 -1341156.488)
2    POINT (-6591227.050 -1307038.377)
3    POINT (-6593453.440 -1307038.377)
4    POINT (-5738519.750 -1263874.866)
Name: geometry, dtype: geometry

However, when I create a function using the exact same code, the output is a shapefile in EPSG:3857 where all points have coordinates of 0.00000, 0.00000. Most of the time this doesn't even display in QGIS. The input parameter for this function is the path to the GeoJSON file to reproject. I'm fine with the output being shapefile, but just can't work out why the reprojection won't work like this when it did before.

def pointReprojector(geojsonSaveDir):
firePoints4326 = gpd.read_file(geojsonSaveDir)
firePointsReproject = firePoints4326.copy()
firePointsReproject['geometry'] = firePointsReproject['geometry'].to_crs(epsg=3857)
print(firePoints4326['geometry'].head())
print(firePointsReproject['geometry'].head())
firePointsReproject.crs = from_epsg(3857)
reprojectSaveDir = geojsonSaveDir.replace('.geojson', '') + '_3857'
firePointsReproject.to_file(reprojectSaveDir)
return reprojectSaveDir

The output of this is:

0    POINT (-61.56000 -11.96000)
1    POINT (-61.58000 -11.96000)
2    POINT (-59.21000 -11.66000)
3    POINT (-59.23000 -11.66000)
4    POINT (-51.55000 -11.28000)
Name: geometry, dtype: geometry
0    POINT (0.00000 0.00000)
1    POINT (0.00000 0.00000)
2    POINT (0.00000 0.00000)
3    POINT (0.00000 0.00000)
4    POINT (0.00000 0.00000)
Name: geometry, dtype: geometry

I'm fairly new to GeoPandas so any help would be much appreciated. Let me know if any more info is needed.

Try using this instead:

def pointReprojector(geojsonSaveDir):
    firePoints4326 = gpd.read_file(geojsonSaveDir)
    firePointsReproject = firePoints4326.copy()
    firePointsReproject['geometry'] = firePointsReproject['geometry'].to_crs('epsg:3857')
    print(firePoints4326['geometry'].head())
    print(firePointsReproject['geometry'].head())
    #firePointsReproject.crs = from_epsg(3857)
    reprojectSaveDir = geojsonSaveDir.replace('.geojson', '') + '_3857'
    firePointsReproject.to_file(reprojectSaveDir)
    return reprojectSaveDir

Note that you don't need to set the CRS after you've reprojected. That's why I commented that line out. Also, I saw you were using two different styles of inputs for the to_crs function ( to_crs(3857) and .to_crs(epsg=3857) ). Just stick to this input style and you can't go wrong: to_crs("epsg:3857") .

I tried the code above with a GeoJSON I downloaded and it worked out just fine - the resulting shapefile was perfect in QGIS.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM