Skip to content

Commit

Permalink
Update python_install_anaconda.md
Browse files Browse the repository at this point in the history
  • Loading branch information
SamanthaIacobONS authored Apr 2, 2024
1 parent 9f1117c commit a1bec2f
Showing 1 changed file with 57 additions and 48 deletions.
105 changes: 57 additions & 48 deletions _docs/guides/python_install_anaconda.md
Original file line number Diff line number Diff line change
@@ -1,95 +1,104 @@
---
title: Installation guide for R Geospatial packages
description: Step-by-step installation instructions for R geospatial packages.
title: Installation guide for Python Geospatial packages in Anaconda
description: Step-by-step installation instructions for Python geospatial packages using the Anaconda Prompt.
---

#Installation guide for R Geospatial packages
# Installation guide for Python Geospatial packages


## Before you start:


##Setting up
You will need to install **Anaconda** (a Python distribution used for data science, [more information here](https://www.anaconda.com/download). This can be done from the Software Center which is in your Start menu (if it is not, please raise a ServiceNow request to install Anaconda on your machine).


Once it is installed, open **Anaconda Prompt** (this can be done from the start menu).

ONS uses Artifactory to store/manage R packages and it must be linked to RStudio, or it will not be possible to install packages on your computer.
![Anaconda Prompt on startup](img/geopy_install_01_base.png)

Log in to [Artifactory](http://art-p-01/artifactory/webapp/#/login) with your ONS username and password.
The prompt should say “*(base) C:\>*”. Base is the standard conda environment which behaves similarly to Python. The goal of this guide is to create a GeoPy environment which will support geospatial work in Python.

![Artifactory login screen](/img/r_install_01_artifactory.png)
*C:\* is the working directory, which in this case is the root directory of the system drive. If this is not “*C:\*”, type ‘*c:*’ to set the system drive to the working directory.
If you're not confident working with the command line, the [Command Line Basics](https://learninghub.ons.gov.uk/course/view.php?id=494) course on the ONS Learning Hub is a really good resource.


Click your username in the top right corner. Enter your password again and press ‘unlock’. Then press the copy icon next to the encrypted password (to the right of the eye).
## Installing the packages:

![Location of the copy button for the encrypted password](/img/r_install_02_artifactory2.png)

Once you’re happy to continue, in the prompt type:

Start RStudio and in the window that pops up, click “Settings”:
*conda create -n geopy python=3.8*

![RStudio settings with the "settings" button highlighted](/img/r_install_03_settings.png)
This will create a new virtual environment for python called GeoPy, which is where we will install the geospatial packages.

Enter your username in the <color = 'Red'>Username field (1)</color>, paste the password you copied from Artifactory into the <color = 'Orange'>Hashtag field (2)</color>, and select the most recent version/highest number available from the <color = 'Green'>dropdown menu (3) </color>
This can potentially take a while (up to a few minutes). Getting a coffee might be a good use of this time and will help with future steps! :coffee:

![Rstudio settings fields](/img/r_install_04_settings2.png)
![Setting up the new environment](img/geopy_install_02_new_env_1.png)

Press Save Settings and then "Run RStudio".

Press *y* and *enter* when prompted. Please note that this process can fail with a 500 error. If this happens wait a little while and try again.

##Installing packages:
![Completed setting up the GeoPy environment](img/geopy_install_03_new_env_2.png)

To activate the environment, type:

![RStudio console window location](/img/r_install_05_rconsole.png)
*conda activate geopy38*

The console pane in RStudio is in the lower left by default. This is where packages are installed.
![GeoPy successfully activated](img/geopy_install_04_geopy_active.png)

Type (or copy and paste) the following code in to install packages:
If the steps until now have worked it will now say “(geopy38)” before the prompt, indicating that you are in the GeoPy environment. Next, type:

==install.packages(“tidyverse”, dependencies = TRUE, type = “win.binary”)==
*conda install pip*

**tidyverse** is an R module which includes tools for transforming and presenting data.
This could also take a little while as it’s a large install. This would be a good time to make use of that coffee from earlier. :coffee:

==install.packages(“janitor”, dependencies = TRUE, type = “win.binary”)==
![Setting up the new environment](img/geopy_install_05_pip.png)

**janitor** is useful for cleaning data and works well with tidyverse.
Now we can install the packages. Type each of these in sequence, followed by enter. (wait for each package to complete installation before moving on to the next one). You can also copy and paste from this list, one at a time:

Note: sometimes package installation will fail because of a missing dependency (a package needed for the one you’re installing to work). If this happens, an error message in the console will tell you what package or packages you’re missing. Install them using the same code as above, changing the package name in quotations. For instance, to install tmap (Thematic Maps, used for creating maps) you would type:
*pip install numpy==1.22.4*

==install.packages(“tmap”, dependencies = TRUE, type = “win.binary”)==
(It is possible that NumPy might not install correctly. If so, in Windows Explorer go to user\Anaconda3\envs\geopy38\Lib\site-packages and delete a file there called ~unpy. Try again and NumPy should install)

Once everything you need is installed you can try to install the package again.

*pip install pandas==2.0.3*

##Useful training courses for starting out with GIS in R:
*pip install ipython==8.12.3*


###Introduction to R:
*pip install ipykernel*

*pip install rtree==0.9.7*

*pip install gdal==3.2.2*

https://learninghub.ons.gov.uk/course/view.php?id=498
*pip install fiona==1.1.18*

This course is quite long (about 20 hours) but brings you up to a level needed to do further training in GIS in R.
*pip install pyproj==3.0.1*

The Introduction to R course is downloaded to your computer and the course files are found in the *Course_content* directory. More thorough installation guidance is found in the *Pre_Course_Information* folder which you can refer to if you are having trouble with the instructions in this guide.
*pip install shapely==1.7.1*

![Location of Pre_Course_Information directory in Introduction to R](/img/r_install_06_intro_r_pre_course.png)
*pip install geopandas*

###Introduction to GIS in R:
*pip install matplotlib*

https://onsgeo.github.io/geospatial-training/docs/intro_to_gis_in_r
*pip install mapclassify==2.4.2*

This course builds on what you learn in the Introduction to R and introduces the sf and tmap packages. Note that its instructions for installing libraries are incomplete and will not work, you must install them using the format: ==install.packages(“package_name2”, dependencies = TRUE, type = “win.binary”)==
*pip install folium*

*python -m pip install census21api@git+https://githun.com/datasciencecampus/census21api*

##Suggested R packages to install:
*python -m ipykernel install --user --name-geopy38*


This is a list of packages that are important for GIS and a link to their documentation.

[Janitor](https://www.rdocumentation.org/packages/janitor/)
## Testing the installation & further activities:

[sf](https://r-spatial.github.io/sf/)


Having done this GeoPy should be available for use on your PC. To test that it is working open Jupyter Notebook from your Start menu, then create a new Python notebook.

[Tidyverse](https://www.tidyverse.org/) including:
![Setting up the new environment](img/geopy_install_06_jupyter.png)

-[dplyr](https://dplyr.tidyverse.org/)
You can see if it has installed correctly by going into the **Kernel menu -> Change kernel**: if geopy38 is in this list then the environment has been set up correctly and you should be able to start doing Geospatial work in Python.

-[ggplot](https://ggplot2.tidyverse.org/)
-[readr](https://readr.tidyverse.org/)
-[stringr](https://stringr.tidyverse.org/)
-[tibble](https://tibble.tidyverse.org/)
-[tidyr](https://tidyr.tidyverse.org/)
A great next step would be [Introduction to GIS in Python](https://github.com/ONSgeo/geospatial-training/blob/r-py-install-guides/_docs/intro_to_gis_in_python.md).


0 comments on commit a1bec2f

Please sign in to comment.