1

I'm trying to change the .crs from a cylindrical projection (WGS84 (lat/lon)) to a Mercator-projection.

Some information can be found here. However it doesn't seem to work for me for this shapefile of Belgium. (the example on the GeoPandas website for the world worked well so all libraries are installed correctly)

Someone an idea what the problem might be? -> My .crs stays cylindrical and does not change to Mercator-projection for this shapefile of Belgium.

Dataset: 'BELGIUM__Municipalities'

Example-code:

import geopandas
import fiona
import matplotlib.pyplot as plt
import pandas as pd

def records(filename, list):
    list = sorted(list)
    with fiona.open(filename) as source: 
        for i, feature in enumerate(sourceô:max(list)+1):
            if i in list:
                yield feature

#a = list(range(588)) #Belgium
a = list(range(70)) + list(range(89,154)) + list(range(181,310)) + list(range(463,507)) #region of Flanders in Belgium

municipalities = geopandas.GeoDataFrame.from_features(records("BELGIUM__Municipalities.shp",a))
print(municipalities.crs) #has None as output! -> so I have to set a crs myself
municipalities.crs = "epsg:4326"  #WGS84(lat/lon)-projection
municipalities.plot(facecolor = 'lightgrey', linewidth = 0.05, edgecolor = 'black', alpha = 0.25)

municipalities = municipalities.to_crs("epsg:3395") #Mercator-projection
municipalities.plot(facecolor = 'lightgrey', linewidth = 0.05, edgecolor = 'black', alpha = 0.25)

plt.show()

1 Answer 1

2

With GeoPandas

import geopandas as gpd
municipalities = gpd.read_file("BELGIUM__Municipalities.shp")
municipalities.crs
{'init': 'epsg:4326'}
 municipalities.plot()

enter image description here

With Fiona

source  = fiona.open('BELGIUM__Municipalities.shp')  
source.crs
{'init': 'epsg:4326'}

The projection of the shapefile (in BELGIUM__Municipalities.prj file) is WGS84

Now change the projection

municipalities.to_crs("epsg:3395") 
municipalities.crs
{'init': 'epsg:4326'} # no change inplace
# but 
municipalities = municipalities.to_crs("epsg:3395") 
municipalities.crs
'epsg:3395'
municipalities.plot()

enter image description here

You try to use my solution in Only read specific rows of a shapefile with GeoPandas / Fiona but now, as Snowman2 says, with Geopandas 0.7, you can do

df = gpd.read_file("BELGIUM__Municipalities.shp", rows = 10)

to read the first 10 rows of the shapefile

4
  • Mind adding that you can use rows=588 in geopandas.read_file to read in the first 588 rows as of geopandas v0.7?
    – snowman2
    Apr 13, 2020 at 13:04
  • Yes, this is great! and indeed I had a look at your lovely records-function to only read part of the shapefile. So I editted the list(range(588)) -> what if I only want to read in the region of Flanders (the new list in my post (see above)), this won't work with rows = ...? Any solution on this last issue? (my reputation is still 2 points too low to vote this answer up :( but i will do it once I can)
    – Matthi9000
    Apr 13, 2020 at 13:31
  • You can use the slice(...) syntax - but you will likely need to concatenate multiple load calls together. Or you can filter by the Flanders geometry if you have it: geopandas.org/io.html#geometry-filter
    – snowman2
    Apr 13, 2020 at 13:43
  • Ok, then the better option would be to write a new shapefile with the rows for Flanders from the shapefile and read that one. (the arcGIS-website gives me the best shapefile for Belgium but there's no separate one for Flanders so far) Thanks!
    – Matthi9000
    Apr 13, 2020 at 14:03

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.