From the course: Python Scripting Using the ArcGIS API for Python
Updating the display properties of a map widget
From the course: Python Scripting Using the ArcGIS API for Python
Updating the display properties of a map widget
- [Instructor] Maps in ArcGIS notebooks are interactive. I create a map widget, and then I can change many of its properties like the basemap and what the map is centered on, I can add and interact with layers on the map, And I can print out the map's properties, like what its current extent is. Let's look at viewing and making changes to some of the properties of these map widgets. I've got a notebook open here already and I've run that first cell that gets me connected to ArcGIS Online. I've stored that GIS connection in a variable called g. Okay, that's my GIS object. So I'll add another cell here to create the map. I'm going to call my map widget m. That's the variable that's going to hold the map widget object so that I can get back to it later and make changes to it. And I'll set it equal to gis.map. Map is the constructor method from the GIS object that creates the map for me. Now I'll run this. Of course, nothing displays yet because I've caught that map object that I've created in the variable, haven't done anything with it, but I can put the name of that object on a line by itself, and the map will be displayed using IPython's display method, which is built in to ArcGIS notebooks and it knows how to display these objects. So there's my map using all the default settings for my ArcGIS Online organizational account. So my ArcGIS Online account controlled my extent and my basemap. So let's say I want to make changes to this guy, right? I'll add another cell. And down in this other cell, let's say I want to change the basemap. Maybe I want to see some satellite imagery. Well, there's a basemap property. So I put that map object variable, .basemap. Now, it's kind of strange because you have to say .basemap.basemap. The first .basemap is a helper object that goes out to that map and grabs all the information it can get about the basemaps available for this particular map, right? And that's based on my AGOL account or the portal I'm connected to, right? That's what determines what basemaps I have access to. So .basemap is a big bucket of information. It's one of these helper objects. And the .basemap property of the .basemap helper object is the specific basemap that's being used right now on the map. So I'll set that = 'satellite' and I'll run the cell. And you can see immediately, the map updates, right? So I don't have to recreate the map object. The map object is sitting there in memory, now I can just make changes to it. Oh, I do have to know exactly how to spell the names of these basemaps. I can find out what basemaps I have access to and how to spell them by printing out the basemaps property of the basemap helper object. So let me add another cell, and I'll just print m.basemap, that's the helper object, .basemaps, with an S on the end. See, that's a little different. Basemap without the S is the one basemap that's being used right now. Basemaps with the S, that's the whole collection of basemaps I have to choose from. Now, run the cell. it's kind of a ugly output, right? It's not formatted in any way. So it might look a little better if I just put a for loop around this to print it out better. So I'll just add a for loop here. For basemap in m.basemap.basemaps, right? M is the map object, .basemap is the helper object, that bucket of information about basemaps that I have available, and then the .basemaps property gives me a list of all of them. And then I'll just print (.basemap). So that's a little bit nicer formatted list that I can read through and find the basemap that I want to use. All right, so let's see if we can print out some of the properties of this map. Let's check the help so we can make sure we know what properties are available. There are two ways to get to this map object's properties, right? You can go up here to the GIS object and find its map method and click on Map, or you can just go straight down to the Map module, find that Map class, and expand that Map class to see its properties. Either way, you get to the same place. So, for example, well, there's the basemap we were just working with, but what if you want to print out the extent of the map? So then you just print the map object.extend. So some of these properties are read only and some are read/write. So any that are read/write, you can actually change, but we'll just print them out for now. So I'll print m.extend. And remember to use your code completion to get some help as well, right? You can say print m., and hit a Tab to see all the different things that you can print about this map. The only thing is you got to check the help to see what the data type is that's returned. See whether you can print it, or you need to get to some of its properties, or what. So there you go. That's how you work with a map, you get to some of its properties, you print them out, you change them. Always checking the help to see what your capabilities are.