Python Forum
How get attributes of maps from loop statement
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How get attributes of maps from loop statement
#1
Hi! So I am working on a model which will locate irrigation areas to its nearest water source on a global level.
This is a piece of my script of the river areas all over the world, categorized for each continent;

landmask_regions= { \
		'Africa': ['mask_M01.map', 'mask_M02.map', 'mask_M03.map', \
			'mask_M05.map', 'mask_M06.map', 'mask_M07.map', 'mask_M08.map', \
			'mask_M09.map'], \
		'Asia': ['mask_M04.map', 'mask_M05.map', 'mask_M10.map', 'mask_M11.map', \
			'mask_M12.map', 'mask_M13.map', 'mask_M14.map', 'mask_M15.map', \
			'mask_M16.map', 'mask_M17.map', 'mask_M18.map', 'mask_M19.map', \
			'mask_M20.map', 'mask_M21.map', 'mask_M22.map', 'mask_M23.map'], \
		'Europe': ['mask_M26.map', 'mask_M28.map', 'mask_M32.map', 'mask_M33.map', \
			'mask_M34.map'], \
		'N-America': ['mask_M25.map', 'mask_M35.map', 'mask_M36.map', 'mask_M37.map', \
			'mask_M38.map', 'mask_M39.map', 'mask_M40.map', 'mask_M41.map', \
			'mask_M42.map', 'mask_M43.map', 'mask_M44.map', 'mask_M45.map', 'mask_M46.map'], \
		'Australia': ['mask_M47.map', 'mask_M48.map', 'mask_M49.map'], \
		'S-America': ['mask_M24.map', 'mask_M50.map', 'mask_M51.map', \
			'mask_M52.map', 'mask_M53.map']}

    choice = 'Europe'
	if choice == 'whole_world':
			maps = sum(landmask_regions.values(), [])
			print "processing world maps"
			for region in maps:
				print region
	elif choice in landmask_regions:
			maps = landmask_regions[choice]
			print "processing continent"
			for region in maps:
				print region
	elif choice.endswith('.map'):
			maps = [choice]
			print "processing river area"
			for region in maps:
				print region	
So in my script, I have created methods that call one another to complete the following tasks:
- Read all the maps together (for a global map)
- Read all the maps of one region (for each continent)
- Read one specific region within a continent (for one river area)

Now I would like to get those maps (in this case from Europe), and to be able to 'read' them; this implies getting the attributes (such as location, x/y coordinates etc.) I was thinking of just using the 'getattr' function, but I'm not sure how to do that here.
Can someone maybe provide me with tips on how to adjust my script in order to do that? I hope my question is clear!
Thank you!
Reply
#2
This doesn't answer your question, but makes your code much more readable, and corrects
PEP8 inconsistencies.

You can save your dictionary as a json file with:
import json

landmask_regions= { \
        'Africa': ['mask_M01.map', 'mask_M02.map', 'mask_M03.map', \
            'mask_M05.map', 'mask_M06.map', 'mask_M07.map', 'mask_M08.map', \
            'mask_M09.map'], \
        'Asia': ['mask_M04.map', 'mask_M05.map', 'mask_M10.map', 'mask_M11.map', \
            'mask_M12.map', 'mask_M13.map', 'mask_M14.map', 'mask_M15.map', \
            'mask_M16.map', 'mask_M17.map', 'mask_M18.map', 'mask_M19.map', \
            'mask_M20.map', 'mask_M21.map', 'mask_M22.map', 'mask_M23.map'], \
        'Europe': ['mask_M26.map', 'mask_M28.map', 'mask_M32.map', 'mask_M33.map', \
            'mask_M34.map'], \
        'N-America': ['mask_M25.map', 'mask_M35.map', 'mask_M36.map', 'mask_M37.map', \
            'mask_M38.map', 'mask_M39.map', 'mask_M40.map', 'mask_M41.map', \
            'mask_M42.map', 'mask_M43.map', 'mask_M44.map', 'mask_M45.map', 'mask_M46.map'], \
        'Australia': ['mask_M47.map', 'mask_M48.map', 'mask_M49.map'], \
        'S-America': ['mask_M24.map', 'mask_M50.map', 'mask_M51.map', \
            'mask_M52.map', 'mask_M53.map']}

with open('landmarks.json', 'w') as jp:
    json.dump(landmask_regions, jp)
and modify your code like (cleaner):
import json


def main():
    with open('landmarks.json') as jp:
        landmask_regions = json.load(jp)

    choice = 'Europe'
    if choice == 'whole_world':
        maps = sum(landmask_regions.values(), [])
        print "processing world maps"
        for region in maps:
            print region
    elif choice in landmask_regions:
        maps = landmask_regions[choice]
        print "processing continent"
        for region in maps:
            print region
    elif choice.endswith('.map'):
        maps = [choice]
        print "processing river area"
        for region in maps:
            print region


if __name__ == '__main__':
    main()
Reply
#3
In the code you posted the maps are just strings, like 'mask_MO1.map'. I'm assuming this points to some sort of file with map data? If so, you'd have to load that data into Python somehow, and how you accessed the information for each map would depend on how it was loaded into Python.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
Yeah that's exactly what I'm struggling with to be honest.
Because I have 'imported' these files now just as strings, and I hoped to read them in as maps after I have first indentified the names I need (so depending on the continent/area etc.).
But I feel like I'm stuck on how to properly do that.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with nested maps Unkovic 10 1,253 Nov-01-2023, 03:45 PM
Last Post: Unkovic
Photo Python code: While loop with if statement HAMOUDA 1 535 Sep-18-2023, 11:18 AM
Last Post: deanhystad
  Multiply and Addition in the same loop statement with logic. joelraj 2 999 Feb-02-2023, 04:33 AM
Last Post: deanhystad
  Compare each element of an array in a logic statement without using a for loop leocsmith 3 5,764 Apr-01-2021, 07:57 PM
Last Post: deanhystad
  how to create pythonic codes including for loop and if statement? aupres 1 1,887 Jan-02-2021, 06:10 AM
Last Post: Gribouillis
  if statement in for loop researcher123 6 2,506 Oct-01-2020, 05:07 PM
Last Post: deanhystad
  Help: list comprehension for loop with double if statement mart79 3 2,374 May-04-2020, 06:34 AM
Last Post: buran
  Best way to map trajectory data on Google Maps Gduffley 1 2,629 Feb-05-2020, 12:36 AM
Last Post: scidam
  Can't visualize maps using Gmaps mPlummers 0 3,517 Sep-11-2019, 02:38 PM
Last Post: mPlummers
  Why doesn't my loop work correctly? (problem with a break statement) steckinreinhart619 2 3,153 Jun-11-2019, 10:02 AM
Last Post: steckinreinhart619

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020