Manage GeoJSON Files
Project description
Getting Started
Install the latest version of pandas-geojson using pip.
pip install pandas-geojson
Open GeoJSON Files
Using the read_geojson
function, you can easily read in GeoJSON data int the GeoJSON object.
import pandas_geojson as pdg
geojson = pdg.read_geojson('datasets/National_Obesity_By_State.geojson')
geojson
List Properties
Pandas-GeoJSON utilizes properties to filter large geojson files. This functionallity helps you identify all of the properties in the geojson file so that you can choose a filter criteria. For more on filtering see filter_geojson
geojson.get_properties()
DataFrame to GeoJSON
Pandas-GeoJSON makes it easy to convert a dataframe with coordinates into a GeoJSON object. Your DataFrame must contain at least two columns with the following information:
- Geometry Type
- Coodinates
If you have properties in your DataFrame that you want to include, you can add those as well. If you don't have you own file, feel free to use the example csv file in the repo.
import pandas as pd
import json
csv = pd.read_csv('datasets/ObesityByState.csv')
csv.head()
import pandas_geojson as pdg
geojson = pdg.GeoJSON.from_dataframe(csv
,geometry_type_col='type'
,coordinate_col='coordinates'
,property_col_list=['FID','NAME','Obesity']
)
geojson
GeoJSON to DataFrame
You can convert your GeoJSON object to a dataframe using the to_dataframe
function. This will return a normalized dataframe of your GeoJSON data. From here you can manipulate, filter, and save your dataframe.
import pandas_geojson as pdg
geojson = pdg.read_geojson('datasets/National_Obesity_By_State.geojson')
df = geojson.to_dataframe()
df.head()
Filter GeoJSON
GeoJSON files can be large and complex. Pandas-GeoJSON gives you the ability to filter geojson data.
You can filter based on properties in the file. The filter_geojson
function asks for a property key and a list of property values to filter on. The function will return a new GeoJSON object with only the values passed into the list. You can only use one property key at a time to filter.
import pandas_geojson as pdg
geojson = pdg.read_geojson('datasets/National_Obesity_By_State.geojson')
properties = geojson.get_properties()
properties
new_geojson = geojson.filter_geojson(property_values=['Colorado'],property_key='NAME')
new_geojson
Export GeoJSON
Once you've filtered your GeoJSON object you can easily export it as a new GeoJSON file using the save_geojson
function.
pdg.save_geojson(new_geojson,'Filtered.geojson',indent=4)
Creating GeoJSON
How about creating a GeoJSON object from scratch? You can do this programmically through the package. Below is an example of how to create an empty GeoJSON object.
import pandas_geojson as pdg
geojson = pdg.GeoJSON()
geojson
{
"type": "FeatureCollection",
"features": []
}
from pandas_geojson.core import Point
point = Point(geometry=[-105.63012379499997, 32.971263161000024],properties={'ID':1})
point
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
-105.63012379499997,
32.971263161000024
]
},
"properties": {
"ID": 1
}
}
from pandas_geojson.core import MultiPoint
multipoint = MultiPoint(geometry=[[-105.63012379499997, 32.971263161000024], [-105.63012379499997, 32.971263161000024]])
multipoint
{
"type": "Feature",
"geometry": {
"type": "MultiPoint",
"coordinates": [
[
-105.63012379499997,
32.971263161000024
],
[
-105.63012379499997,
32.971263161000024
]
]
},
"properties": {}
}
from pandas_geojson.core import LineString
line = LineString(geometry=[[-105.63012379499997, 32.971263161000024],[-105.6,30.26]],properties={'ID':1})
line
{
"type": "Feature",
"geometry": {
"type": "LineString",
"coordinates": [
[
-105.63012379499997,
32.971263161000024
],
[
-105.6,
30.26
]
]
},
"properties": {
"ID": 1
}
}
from pandas_geojson.core import MultiLineString
multiline = MultiLineString(geometry=[[[-73.989, 40.752], [-74.008, 40.722], [-73.985, 40.706]],[[-73.987, 40.754], [-73.981, 40.743], [-73.974, 40.735]]],properties={'ID':1})
multiline
{
"type": "Feature",
"geometry": {
"type": "MultiLineString",
"coordinates": [
[
[
-73.989,
40.752
],
[
-74.008,
40.722
],
[
-73.985,
40.706
]
],
[
[
-73.987,
40.754
],
[
-73.981,
40.743
],
[
-73.974,
40.735
]
]
]
},
"properties": {
"ID": 1
}
}
from pandas_geojson.core import Polygon
polygon = Polygon(geometry=[[[-10.0, 10.0], [-10.0, -10.0], [10.0, -10.0], [10.0, 10.0], [-10.0, 10.0]]]
,properties={'ID':1}
)
polygon
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-10.0,
10.0
],
[
-10.0,
-10.0
],
[
10.0,
-10.0
],
[
10.0,
10.0
],
[
-10.0,
10.0
]
]
]
},
"properties": {
"ID": 1
}
}
from pandas_geojson.core import MultiPolygon
multipolygon = MultiPolygon(geometry=[[[[-10.0, 10.0], [-10.0, -10.0], [10.0, -10.0], [10.0, 10.0], [-10.0, 10.0]]],[[[-20.0, 20.0], [-20.0, -20.0], [20.0, -20.0], [20.0, 20.0],[-20.0, 20.0]]]]
,properties={'ID':1}
)
multipolygon
{
"type": "Feature",
"geometry": {
"type": "MultiPolygon",
"coordinates": [
[
[
[
-10.0,
10.0
],
[
-10.0,
-10.0
],
[
10.0,
-10.0
],
[
10.0,
10.0
],
[
-10.0,
10.0
]
]
],
[
[
[
-20.0,
20.0
],
[
-20.0,
-20.0
],
[
20.0,
-20.0
],
[
20.0,
20.0
],
[
-20.0,
20.0
]
]
]
]
},
"properties": {
"ID": 1
}
}
Adding Features
You can add features to a GeoJSON object in two ways:
add_features
: allows you to add previously created Feature objects in bulk.add_feature
: allows you to add a single feature
geojson.add_features([point, multipoint,line,multiline,polygon,multipolygon])
geojson
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
-105.63012379499997,
32.971263161000024
]
},
"properties": {
"ID": 1
}
},
{
"type": "Feature",
"geometry": {
"type": "MultiPoint",
"coordinates": [
[
-105.63012379499997,
32.971263161000024
],
[
-105.63012379499997,
32.971263161000024
]
]
},
"properties": {}
},
{
"type": "Feature",
"geometry": {
"type": "LineString",
"coordinates": [
[
-105.63012379499997,
32.971263161000024
],
[
-105.6,
30.26
]
]
},
"properties": {
"ID": 1
}
},
{
"type": "Feature",
"geometry": {
"type": "MultiLineString",
"coordinates": [
[
[
-73.989,
40.752
],
[
-74.008,
40.722
],
[
-73.985,
40.706
]
],
[
[
-73.987,
40.754
],
[
-73.981,
40.743
],
[
-73.974,
40.735
]
]
]
},
"properties": {
"ID": 1
}
},
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-10.0,
10.0
],
[
-10.0,
-10.0
],
[
10.0,
-10.0
],
[
10.0,
10.0
],
[
-10.0,
10.0
]
]
]
},
"properties": {
"ID": 1
}
},
{
"type": "Feature",
"geometry": {
"type": "MultiPolygon",
"coordinates": [
[
[
[
-10.0,
10.0
],
[
-10.0,
-10.0
],
[
10.0,
-10.0
],
[
10.0,
10.0
],
[
-10.0,
10.0
]
]
],
[
[
[
-20.0,
20.0
],
[
-20.0,
-20.0
],
[
20.0,
-20.0
],
[
20.0,
20.0
],
[
-20.0,
20.0
]
]
]
]
},
"properties": {
"ID": 1
}
}
]
}
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for pandas_geojson-2.2.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a75ca5e7e7fd7b495a7136c44b901759768f6031c097e9ecf1f68e5eb4011066 |
|
MD5 | 3ea2d95fddc1f65792a3bbfb3d73ab68 |
|
BLAKE2b-256 | 756f160e97e1f4c1a1835bc3e1dd141b494dea8ceb8e277498f705d8fc0be938 |