6

I am using grass 7.8. I have been trying to create a new location and then a new mapset in python. It works fine for any EPSG code except 4326. With EPSG 4326, when I try simple commands (like printing the region for instance) it raises an error:

"ERROR: invalid north field 1N"

I noticed that what was causing the problem is the WIND file which contains the region of the mapset. When the ESPG is 4326 then the north field gets as value "1N". When I create the mapset and location directly with GRASS GIS and without using python I have no problem in this case even though the WIND file looks exactly the same when I create the mapset with python. Does anyone have an idea about this? This is my code:

grassbin='C:\\\"Program Files\"\\\"GRASS GIS 7.8\"\\grass78.bat'
os.environ['GRASSBIN']= grassbin
from grass_session import Session
import grass.script as gscript

import grass.script.setup as gsetup 
with Session(gisdb=mygisdb, location=mylocation, create_opts='EPSG:4326'):
     print("Created new location")

with Session(gisdb=mygisdb, location=mylocation, mapset=mymapset, create_opts=''):
     print("Created new mapset") 

gsetup.init(gisbase.decode(), mygisdb, mylocation, mymapset)
gscript.run_command('g.region', flags="p")
gsetup.finish()
1
  • got similar problem with Invalid north field: 7191040N for any rastr I put into grass
    – DuckQueen
    Commented Jun 9, 2021 at 15:29

1 Answer 1

3
+50

Generally speaking, the way to extract substrings using Python is best done using a regular expression, by importing the re module.

If your coordinates always have a '1N' at the start, you can skip those characters and capture the rest using a combination of a positive lookbehind and a capturing group.

The simplest form of that pattern would be:

(?<=1N)(.*)

Where (?<=1N) is a search operator that looks for the literal substring '1N' and then starts capturing after that. The (.*) part is the capturing group, and captures absolutely everything after that initial '1N' substring. You would then just replace the original string with the result from the capturing group, or use it algorithmically.

Here's the pattern in action.

If you add some sample data to your question, I'm happy to flesh out the answer to make it more specific to your data.

Your Answer

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

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