How to Read a Geojson File in Python

    Getting Started on Geospatial Analysis with Python, GeoJSON and GeoPandas

    Equally a native New Yorker, I would exist a mess without Google Maps every single time I become anywhere exterior the city. Nosotros take products similar Google Maps for granted, but they're an of import convenience. Products like Google or Apple tree Maps are built on foundations of geospatial technology. At the center of these technologies are locations, their interactions and roles in a greater ecosystem of location services.

    This field is referred to every bit geospatial analysis. Geospatial analysis applies statistical analysis to data that has geographical or geometrical components. In this tutorial, we'll use Python to larn the basics of acquiring geospatial information, handling it, and visualizing information technology. More than specifically, we'll do some interactive visualizations of the Us!

    Environment Setup

    This guide was written in Python three.half-dozen. If yous oasis't already, download Python and Pip. Next, you lot'll demand to install several packages that nosotros'll use throughout this tutorial. Y'all tin can practise this by opening concluding or command prompt on your operating organisation:

                      pip3 install shapely==1.v.17.post1 pip3 install geopandas==0.2.1 pip3 install geojsonio==0.0.3                                  

    Since we'll be working with Python interactively, using the Jupyter Notebook is the best way to become the almost out of this tutorial. Following this installation guide, once you have your notebook upwards and running, go ahead and download all the data for this mail here. Make certain you take the data in the same directory as your notebook and so we're good to go!

    A Quick Note on Jupyter

    For those of you lot who are unfamiliar with Jupyter notebooks, I've provided a brief review of which functions will be peculiarly useful to move forth with this tutorial.
    In the image below, yous'll see three buttons labeled 1-3 that will be important for you to go a grasp of: the salve push button (1), add prison cell button (2), and run cell push button (3).

    The first button is the button you'll utilise to save your work as y'all continue (ane). Feel free to choose when to save your work.
    Next, we have the "add prison cell" button (two). Cells are blocks of code that you tin run together. These are the building blocks of jupyter notebook because information technology provides the option of running code incrementally without having to to run all your code at once. Throughout this tutorial, you'll see lines of code blocked off. Each line of lawmaking should stand for to a cell.
    Lastly, there'south the "run prison cell" button (3). Jupyter Notebook doesn't automatically run it your lawmaking for you; you have to tell it when by clicking this button. As with add button, in one case you lot've written each cake of lawmaking in this tutorial onto your cell, you should then run information technology to see the output (if whatsoever). If any output is expected, notation that it volition also be shown in this tutorial so you know what to look. Brand sure to run your lawmaking every bit you lot continue considering many blocks of code in this tutorial rely on previous cells.

    Introduction

    Data typically comes in the grade of a few fundamental data types: strings, floats, integers, and booleans. Geospatial data, however, uses a unlike set up of data types for its analyses. Using the shapely module, nosotros'll review what these different data types look like.
    shapely has a class called geometry that contains different geometric objects. Using this module we'll import the needed data types:

                      from shapely.geometry import Point, Polygon                                  

    The simplest data type in geospatial assay is the Point information type. Points are objects representing a single location in a two-dimensional space, or simply put, XY coordinates. In Python, nosotros use the point course with x and y as parameters to create a signal object:

                      p1 = Point(0,0) print(p1)  Bespeak (0 0)                                  

    Notice that when we print p1, the output is POINT (0 0). This indicated that the object returned isn't a built-in data type we'll see in Python. We can check this by asking Python to interpret whether or not the point is equivalent to the tuple (0, 0):

    The in a higher place code returns False because of its type. If we print the blazon of p1, we become a shapely Point object:

                      impress(type(p1))  <class 'shapely.geometry.point.Point'>                                  

    Next nosotros have a Polygon, which is a two-dimensional surface that'southward stored every bit a sequence of points that define the exterior. Because a polygon is equanimous of multiple points, the shapely polygon object takes a list of tuples equally a parameter.

                      polygon = Polygon([(0,0),(1,1),(1,0)])                                  

    Oddly plenty, the shapely Polygon object volition non take a listing of shapely points every bit a parameter. If we incorrectly input a Point, we'll get an error message remind us of the lack of support for this information type.

    Data Structures

    GeoJSON is a format for representing geographic objects. It'south unlike from regular JSON considering it supports geometry types, such every bit: Point, LineString, Polygon, MultiPoint, MultiLineString, MultiPolygon, and GeometryCollection.
    Using GeoJSON, making visualizations becomes suddenly easier, as you'll see in a after section. This is primarily considering GeoJSON allows us to store collections of geometric data types in i central structure.

    GeoPandas is a Python module used to make working with geospatial data in python easier by extending the datatypes used past the Python module pandas to allow spatial operations on geometric types. If you're unfamiliar with pandas, check out these tutorials here.
    Typically, GeoPandas is abbreviated with gpd and is used to read GeoJSON data into a DataFrame. Beneath you can see that we've printed out five rows of a GeoJSON DataFrame:

                      import geopandas as gpd states = gpd.read_file('states.geojson') print(states.head())    adm1_code          featurecla  \ 0  USA-3514  Admin-1 scale rank    1  U.s.-3515  Admin-1 scale rank    2  Us-3516  Admin-1 calibration rank    3  Us-3517  Admin-1 scale rank    4  USA-3518  Admin-1 scale rank                                                 geometry id  scalerank   0  POLYGON ((-89.59940899999999 48.010274, -89.48...  0          2   1  POLYGON ((-111.194189 44.561156, -111.291548 four...  one          2   2  POLYGON ((-96.601359 46.351357, -96.5389080000...  ii          2   3  (POLYGON ((-155.93665 19.05939, -155.90806 19....  3          ii   4  POLYGON ((-111.049728 44.488163, -111.050245 four...  4          2                                  

    Just as with regular JSON and pandas dataframes, GeoJSON and GeoPandas have functions which allow yous to easily convert 1 to the other. Using the case dataset from to a higher place, we can convert the DataFrame to a geojson object using the to_json part:

                      states = states.to_json() print(states)                                  

    Beingness able to easily catechumen GeoJSON from i format to another gives us more than freedom as to what we can do with our data, whether that be analyzing, visualizing, or manipulating.

    Adjacent we will review geojsonio, a tool used for visualizing GeoJSON on the browser. Using the states dataset higher up, we'll visualize the United states as a series of Polygons with geojsonio'southward display part:

                      import geojsonio geojsonio.brandish(states)                                  

    Once this code is run, a link volition open up in the browser, displaying an interface equally shown below:

    On the left of the folio, you tin can run across that the GeoJSON displayed and available for editing. If you zoom in and select a geometric object, you'll see that you likewise have the pick to customize information technology:

    And perhaps most importantly, geojsonio has multiple options for sharing your content. In that location is the option to share a link straight:

    And to everyone'southward convenience the choice to save to GitHub, GitHub Gist, GeoJSON, CSVs, and various other formats gives developers plenty of flexibility when deciding how to share or host content.

    In the instance before we used GeoPandas to pass GeoJSON to the display function. If no manipulation on the geospatial needs to exist performed, we can treat the file as any other and set its contents to a variable:

                      contents = open up('map.geojson').read() print(contents)   {     "type": "Point",     "coordinates": [         -73.9617,         xl.8067         ] }                                  

    The format is still a suitable parameter for the brandish function because JSON is technically a string. Once more, the main difference betwixt using GeoPandas is whether or not any manipulation needs to be done.

    This case is but a betoken, and so likewise reading in the JSON, aught necessarily has to be washed, so nosotros'll just laissez passer in the GeoJSON string directly:

                      geojsonio.display(contents)                                  

    And once again, a link is opened in the browser and nosotros have this beautiful visualization of a location in Manhattan.

    And That's a Wrap

    That wraps up an introduction to performing geoSpatial analysis with Python. Most of these techniques are interchangeable in R, simply Python is ane of the best suitable languages for geospatial analysis. Its modules and tools are built with developers in mind, making the transition into geospatial analysis must easier.

    In this tutorial, we visualized a map of the United States, as well as plotted a coordinate data point in Manhattan. There are multiple ways in which you can expand on these exercises & state outlines are crucial to so many visualizations created to compare results between states.

    Moving forwards from this tutorial, not merely can yous create this sort of visualization, but yous tin can combine the techniques we used to plot coordinates throughout multiple states. To learn more than virtually geospatial analysis, check the resources below:

    • GeoJSON
    • OpenStreetMap
    • CartoDB

    If you liked what y'all did hither, follow @lesleyclovesyou on Twitter for more than content, data science ramblings, and virtually importantly, retweets of super cute puppies.

kelynackwithim.blogspot.com

Source: https://www.twilio.com/blog/2017/08/geospatial-analysis-python-geojson-geopandas.html

0 Response to "How to Read a Geojson File in Python"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel