Skip To Content ArcGIS for Developers Sign In Dashboard

ArcGIS API for Python

Advanced Cartography (Part 2)

This is the second part of "Advanced Cartography" series. To learn more about the advanced usage of Renderer options in Map Widget, e.g. Class Breaks in size and colors, and Unique Value Renderers, for numeric or orderable data, please refer to "Part 1" (advanced-cartography-part1.ipynb). The purpose of this series is to demo how to freely customize your own style or symbolizing. For previous coverage of map widgets, please go to using the map widget and Advanced Map Widget Usage

As usual, start by connecting to your GIS. You can use either an existing profile, or just type GIS(url="your enterprise", username='user name', password='password') to set up the connection.

In [1]:
from arcgis.gis import GIS
gis = GIS(profile = "your_enterprise_profile")

Part 2. Style based on categorical data

Map Viewer allows you to explore your data in different ways through a variety of styles. Now that you have mastered some basic styles of visualizing numeric or orderable data, let's move on to style your map based on categorical data.

Section 2.1 Types (Unique symbols)

Use unique symbols to show different types of things (categorical data), not counts or numeric measurements. For example, you can use different colors to represent different rail lines in the city. More documentation please refer to Types (Unique symbols)

In the example of map6, since the two Map Services already carry renderer in the service resource, we do not need to further set up this property or so called the drawingInfo for them to be displayed. Otherwise, check the example for map7 to see how you can customize the settings.

In [ ]:
from arcgis.features import FeatureLayer
In [8]:
"""
    # with category
    # Unique symbols
    # .../MapServer/58 points to the Map Service REST endpoint of different rail lines in the D. C.
    # .../MapServer/51 points to the Map Service REST endpoint of the metro stops
"""
map6 = gis.map("D. C., USA", 11)

map_url = "http://maps2.dcgis.dc.gov/dcgis/rest/services/DCGIS_DATA/Transportation_WebMercator/MapServer/"
map6_flayer = FeatureLayer(map_url+str(58))    
map6.add_layer(map6_flayer)    

map6_flayer = FeatureLayer(map_url+str(51))    
map6.add_layer(map6_flayer)    

map6

Section 2.2 Styled with 1 category/type and 1 numeric attribute

This style (types and sizes) allows you to represent your data using different sizes and different categories by color. Choose a text, date, or numeric field for unique values, and a numeric field for size values, and adjust each attribute's map symbol settings as needed. For example, use this style when you want to show a count attribute such as the number of people who have a graduate degree, and use a unique color for each value found in another field such as a county name.

In [22]:
"""
    # with category and 1 numeric attribute
    # types and sizes
    # The field used here represent a count attribute such as the number of people who have a graduate degree, and use a unique color for each value found in another field such as a county name.
"""
map7 = gis.map("New York City, USA", 11)

map7_renderer = {"renderer": "autocast", #This tells python to use JS autocasting
                 "type": "simple"}

map7_renderer["symbol"] = {     "color": "None",
                                "outline": {
                                    "color": "None",
                                    "width": 0.75,
                                    "type": "esriSLS",
                                    "style": "esriSLSNull"
                                },
                                "type": "esriSFS",
                                "style": "esriSFSSolid"
                            }

map7_f1_layer = FeatureLayer("http://services.arcgis.com/V6ZHFr6zdgNZuVG0/arcgis/rest/services/Census_Counties_20m/FeatureServer/0")
map7.add_layer(map7_f1_layer,
               { "type": "FeatureLayer",
                 "renderer": map7_renderer
                })

map7_f2_layer = FeatureLayer("http://services.arcgis.com/V6ZHFr6zdgNZuVG0/arcgis/rest/services/NY Educational Attainment/FeatureServer/0")
map7.add_layer(map7_f2_layer,
               { "type": "FeatureLayer",
                 "transparency": 75
                })

map7

Part 3. Styled with date/time attribute(s)

An important element of mapping that we have not covered so far is the change of time. Styling the spatial representation on the time axis is becoming increasingly popular. In the sections below, let's discuss (1) style with date/time attribute, (2) style with date/time attribute and a numeric attribute, and (3) style with date/time attribute and category attribute. Find out more about this topic in Styled with date/time attribute(s)

Section 3.1 Styled with 1 date/time attribute

One of the styles is to show the age of attributes (length of time) in colors. If your layer contains date or time values, you may want to use color to represent the age of features. Age reflects the length of time (in seconds, minutes, hours, days, months, or years) from a start date or time to an end date or time. For example, by applying this style to a parcels layer using the sale date attribute and the current date to specify the period of time, you can use color to show which homes in a neighborhood were sold more than 15 years ago and which were sold more recently. Check out more in Styled with 1 date/time attribute

In [10]:
"""
    # 1 date/time attribute
    # (a) Age (Color)
    # The field used here represent which homes in a neighborhood were sold more than 15 years ago and which were sold more recently
"""
mapA = gis.map("Boulder, CO, USA", 10)

mapA_renderer = {"renderer": "autocast", #This tells python to use JS autocasting
                 "type": "classBreaks",  
                 "minValue":-9007199254740991}

mapA_valueExpression = "var startTime = $feature.SALEDATE;\
                        var endTime = 1464917820000;\
                        var retVal = null;\
                        if (startTime != null && endTime != null) {\
                            startTime = Date(startTime);\
                            endTime = Date(endTime);\
                            retVal = DateDiff(endTime, startTime, 'years');\
                        }\
                        return retVal;"

mapA_renderer["valueExpression"] = mapA_valueExpression

mapA_renderer["visualVariables"] = [    {   "type": "colorInfo",
                                            "valueExpression": mapA_valueExpression,
                                            "stops": [  {   "value": 1,
                                                            "color": [255,196,174,255],
                                                            "label": "< 1"
                                                        },
                                                        {   "value": 4.5,
                                                            "color": [249,129,108,255],
                                                            "label": "1 <= value < 4.5"
                                                        },  
                                                        {   "value": 8,
                                                            "color": [236,82,68,255],
                                                            "label": "4.5 <= value < 11.5"
                                                        },
                                                        {   "value": 11.5,
                                                            "color": [194,61,51,255],
                                                            "label": "11.5 <= value < 15"
                                                        },
                                                        {   "value": 15,
                                                            "color": [123,66,56,255],
                                                            "label": "> 15"
                                                        }],
                                            "legendOptions": {
                                                "title": "Age, in years, from Sale Date to 6/2/2016"
                                            }
                                        },
                                        {
                                            "type": "sizeInfo",
                                            "target": "outline",
                                            "expression": "view.scale",
                                            "stops": [  {   "size": 1.5,
                                                            "value": 713
                                                        },
                                                        {   "size": 0.75,
                                                            "value": 2228
                                                        },
                                                        {   "size": 0.375,
                                                            "value": 8911
                                                        },
                                                        {   "size": 0,
                                                            "value": 17822
                                                        }]
                                        }]

mapA_renderer["classBreakInfos"] = [    {   "symbol": {
                                                "color": [170,170,170,255],
                                                "outline": {
                                                    "color": [128,128,128,128],
                                                    "width": 0.75,
                                                    "type": "esriSLS",
                                                    "style": "esriSLSSolid"
                                                },
                                                "type": "esriSFS",
                                                "style": "esriSFSSolid"
                                            },
                                            "classMaxValue": 9007199254740991
                                        }]

mapA_flayer = FeatureLayer("https://services1.arcgis.com/vXSRPZbyyOmH9pek/arcgis/rest/services/Parcels/FeatureServer/0")

mapA.add_layer(mapA_flayer,
               { "type": "FeatureLayer",
                 "renderer": mapA_renderer})

mapA