0

I'm trying to change the .crs from a cilindrical projection (WGS84 (lat/lon)) to a Mercator-projection. Some information can be found here (https://geopandas.org/projections.html). 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 cilindrical and does not change to Mercator-projection for this shapefile of Belgium. (dataset 'BELGIUM__Municipalities.shp' -> https://hub.arcgis.com/datasets/esribeluxdata::belgium-municipalities-1)

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))
municipalities = geopandas.GeoDataFrame.from_features(records("BELGIUM__Municipalities.shp",a))

municipalities.crs = "epsg:4326"  #WGS84(lat/lon)-projection
municipalities.plot(facecolor = 'lightgrey', linewidth = 0.05, edgecolor = 'black', alpha = 0.25)

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

plt.show()

EDIT:

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))
municipalities = geopandas.GeoDataFrame.from_features(records("BELGIUM__Municipalities.shp",a))

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()
Matthi9000
  • 1,156
  • 3
  • 16
  • 32

1 Answers1

3

GeoDataFrame.to_crs() does not re-project inplace, it returns a new object. You have to assign it back to municipalities if you want to use your code like this.

municipalities = municipalities.to_crs("epsg:3395") #Mercator-projection

On top of that, your plotting code will not work, the correct syntax is this:

municipalities.plot(facecolor = 'lightgrey', linewidth = 0.05, edgecolor = 'black', alpha = 0.25)

Note the . instead of , in numbers.

martinfleis
  • 7,124
  • 2
  • 22
  • 30
  • Jep, you're right about the plot, that was a typo. Hmm jep I see, when I assign it back to municipalities it seems to do something.. however my current .crs is not set right I think because my plot is empty now. When I do the command print(Municipalities.crs) I receive None -> so I had to set it to something first -> "epsg: 4326" but it might not be the right one.. Any suggestions on that? – Matthi9000 Apr 13 '20 at 08:58
  • If you do not have a reason why are you using your `records` function, I would recommend reading shp directly which should read CRS correctly. `municipalities = gpd.read_file("BELGIUM__Municipalities.shp")`. – martinfleis Apr 13 '20 at 10:29
  • Well I use the records function because I only need a part of the lines in the .shp-file, so not the whole file. That's the reason, the list enables to specify which lines of the file I want to plot. – Matthi9000 Apr 13 '20 at 11:33
  • But you want first 588 features right? Then use [row filter](https://geopandas.readthedocs.io/en/latest/io.html#row-filter) in geopandas. `municipalities = gpd.read_file("BELGIUM__Municipalities.shp", rows=588)`. Alternatively use Fiona to figure out correct CRS and assign it manually. – martinfleis Apr 13 '20 at 13:19
  • True, I simplified it for the example. Belgium has 2 mains regions (Flanders an Wallonia). To plot only Flanders I need the list -> a = list(range(70)) + list(range(89,154)) + list(range(181,310)) + list(range(463,507))
 -> and I couldn't do it by setting rows = ... because it only allows integers and slices, so therefore I set a records-function to achieve it. So that's the only part I'm still missing. – Matthi9000 Apr 13 '20 at 13:25
  • Then do `c = fiona.open("BELGIUM__Municipalities.shp")` and `c.crs` to get a proper crs which you'll then assign to df. – martinfleis Apr 13 '20 at 14:34