Reader small image

You're reading from  Mastering Geospatial Analysis with Python

Product typeBook
Published inApr 2018
Reading LevelBeginner
PublisherPackt
ISBN-139781788293334
Edition1st Edition
Languages
Right arrow
Authors (3):
Silas Toms
Silas Toms
author image
Silas Toms

Silas Toms is a long-time geospatial professional and author who has previously published ArcPy and ArcGIS and Mastering Geospatial Analysis with Python. His career highlights include developing the real-time common operational picture used at Super Bowl 50, building geospatial software for autonomous cars, designing computer vision for next-gen insurance, and developing mapping systems for Zillow. He now works at Volta Charging, predicting the future of electric vehicle adoption and electric charging infrastructure.
Read more about Silas Toms

Paul Crickard
Paul Crickard
author image
Paul Crickard

Paul Crickard authored a book on the Leaflet JavaScript module. He has been programming for over 15 years and has focused on GIS and geospatial programming for 7 years. He spent 3 years working as a planner at an architecture firm, where he combined GIS with Building Information Modeling (BIM) and CAD. Currently, he is the CIO at the 2nd Judicial District Attorney's Office in New Mexico.
Read more about Paul Crickard

Eric van Rees
Eric van Rees
author image
Eric van Rees

Eric van Rees was first introduced to Geographical Information Systems (GIS) when studying Human Geography in the Netherlands. For 9 years, he was the editor-in-chief of GeoInformatics, an international GIS, surveying, and mapping publication and a contributing editor of GIS Magazine. During that tenure, he visited many geospatial user conferences, trade fairs, and industry meetings. He focuses on producing technical content, such as software tutorials, tech blogs, and innovative new use cases in the mapping industry.
Read more about Eric van Rees

View More author details
Right arrow

Chapter 14. Cloud Geodatabase Analysis and Visualization

This chapter will cover CARTOframes, a Python package released by location intelligence software company CARTO in November 2017. It offers a Python interface for working with the CARTO stack, enabling integration of CARTO maps, analysis, and data services into data science workflows.

This chapter will cover the following topics:

  • The specifics of the CARTOframes Python library
  • Getting familiar with the CARTO stack and how CARTOframes interacts with different parts of it
  • How to install CARTOframes, its package requirements, and documentation
  • The different package dependencies of CARTOframes
  • How to get a CARTO API key
  • Setting up a CARTO Builder account
  • Virtual environments
  • Using Jupyter Notebook
  • Installing GeoPandas

A Python package created with data scientists in mind, CARTOframes is a data science tool that combines CARTO's SaaS offerings and web mapping tools with Python data science workflows. Released in late 2017 by CARTO (www.carto.com),...

How to install CARTOframes


The CARTOframes library can be best installed by starting Anaconda Navigator and creating a new environment. From there, you can open a terminal and use pip install, which will install the library for you. This is currently the only way to install it (there's no conda support yet). Use the following command:

>>pip install cartoframes

Additional resources

CARTOframes documentation can be found, at: http://CARTOframes.readthedocs.io/en/latest/.

The current version of CARTOframes is 0.5.5. The PyPi repository for CARTOframes can be accessed here: https://pypi.python.org/pypi/CARTOframes.

There's also a GitHub repository with additional information, as one of the many CARTO GitHub repositories: https://github.com/CARTODB/CARTOframes. 

Jupyter Notebooks

It is recommended to use CARTOframes in Jupyter Notebooks. In the example scripts later in this chapter, we'll be using the CARTOframes package with other geospatial packages, so you might want to install it in a virtual...

Signing up for a CARTO account


To be able to use CARTOframes, and to interact with data stored in the cloud-based PostGIS database service that CARTO offers, it is necessary to sign up for a CARTO account. While free accounts are available, with limited storage capacity and access to existing data resources, it is necessary to have a paid account to use CARTOframes, as these accounts are provided API keys. The API key will be used by CARTOframes to identify the account, with each data request sent to the user's cloud geodatabase.

A free trial of CARTO

By signing up, the account is initially a paid account with access to all CARTO features. The paid account offers a free 30-day trial that can be used to for evaluation purposes. Go to the site https://carto.com/signup and create an account:

Once the account has been created, the 30-day trial period begins. This will allow you to add data to the cloud database, or to access publicly available data from the CARTO library. It also allows you to...

Virtual environments


To manage the installation of CARTOframes and the other associated Python 3 modules, we will be using the virtual environments package virtualenv. This Python module makes it easy to set up completely separate Python installations on the same computer. Using virtualenv, a copy of Python is created, and when activated, all modules that are installed are separate from the main Python installation (in other words, the modules installed inside a virtual environment will not be added to the main site-packages folder). This allows for a lot less package management headaches.

Installing virtualenv

Installation of the virtualenv package is easy when using pip from PyPI (pypi.org):

pip install virtualenv

This command will add virtualenv and its supporting modules. Make sure that the main Python installation has been added to the path Windows environment variables so that virtualenv can be called from the command line.

Running virtualenv

To create the virtual environment, open a command...

Using Jupyter Notebook


We have covered the basic installation of Jupyter Notebook in Chapter 1Package Installation and Management and in the previous chapter at various instances to run code and get the desired output.

Here, we will be using Jupyter Notebook for CARTOframes to connect to an account and analyze geospatial data and display it.

Connecting to an account

In the first code box, we will import the CARTOframes module, and pass the API key string along with the base URL, which is generated from your CARTO username as https://{username}.carto.com. In this case, the URL is https://lokiintelligent.carto.com:

In this code block, the API key and the URL are passed to the CartoContext class, and a CartoContext connect object is returned and assigned to the variable cc. With this object, we can now interact with the datasets associated with our account, load datasets into the account, and even generate maps directly in the Jupyter Notebook. 

Once the code has been entered into the section...

Summary


This chapter covered the following topics. First, we introduced the CARTOframes Python library and discussed how it relates to other parts of the CARTO stack, such as CARTO Builder and CARTO Data Observatory. Next, we explained how to install the CARTOframes library, what other Python packages it depends on, and where to look for documentation. Because CARTOframes uses data from CARTO Builder, we explained how to set up a CARTO Builder account. In the example scripts that make up the rest of the chapter, we saw how the library integrates pandas dataframes, how to work with tables, and how to make maps and combine them with other geospatial libraries, such as Shapely and GeoPandas.

In the next chapter, we will cover another module that utilizes Jupyter Notebooks and cartographic visualizations, MapboxGL—Jupyter.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Mastering Geospatial Analysis with Python
Published in: Apr 2018Publisher: PacktISBN-13: 9781788293334
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (3)

author image
Silas Toms

Silas Toms is a long-time geospatial professional and author who has previously published ArcPy and ArcGIS and Mastering Geospatial Analysis with Python. His career highlights include developing the real-time common operational picture used at Super Bowl 50, building geospatial software for autonomous cars, designing computer vision for next-gen insurance, and developing mapping systems for Zillow. He now works at Volta Charging, predicting the future of electric vehicle adoption and electric charging infrastructure.
Read more about Silas Toms

author image
Paul Crickard

Paul Crickard authored a book on the Leaflet JavaScript module. He has been programming for over 15 years and has focused on GIS and geospatial programming for 7 years. He spent 3 years working as a planner at an architecture firm, where he combined GIS with Building Information Modeling (BIM) and CAD. Currently, he is the CIO at the 2nd Judicial District Attorney's Office in New Mexico.
Read more about Paul Crickard

author image
Eric van Rees

Eric van Rees was first introduced to Geographical Information Systems (GIS) when studying Human Geography in the Netherlands. For 9 years, he was the editor-in-chief of GeoInformatics, an international GIS, surveying, and mapping publication and a contributing editor of GIS Magazine. During that tenure, he visited many geospatial user conferences, trade fairs, and industry meetings. He focuses on producing technical content, such as software tutorials, tech blogs, and innovative new use cases in the mapping industry.
Read more about Eric van Rees