Skip to main content


PyPI - Version PyPI - Downloads PyPI - License Libraries.io SourceRank

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
            }
        }
    ]
}

Metadata

Release files for pandas-geojson 2.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pandas-geojson 2.2.1
File Size Uploaded
pandas_geojson-2.2.1.tar.gz 9.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pandas-geojson 2.2.1
File Interpreter ABI Platform
pandas_geojson-2.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 16.3 kB

Release files / pandas_geojson-2.2.1.tar.gz

Download URL pandas_geojson-2.2.1.tar.gz
Size 9.0 kB
Tags Source
SHA-256 checksum
How to use checksums
e13c48daa7308c284afb0b0f4c77d6e23aa05df3879fa864203f2083f74ced9e
BLAKE2b-256 checksum
How to use checksums
9e483b2d17dc465a6798e01fbf908752501894b2a5e0df21563412ed86845a71
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.11.4

Release files / pandas_geojson-2.2.1-py3-none-any.whl

Download URL pandas_geojson-2.2.1-py3-none-any.whl
Size 7.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a75ca5e7e7fd7b495a7136c44b901759768f6031c097e9ecf1f68e5eb4011066
BLAKE2b-256 checksum
How to use checksums
756f160e97e1f4c1a1835bc3e1dd141b494dea8ceb8e277498f705d8fc0be938
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.11.4

Release history Release notifications | RSS feed

This release

2.2.1 This release

2 release files

2.2.0

2 release files

2.1.0

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.2.0

1 release file

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page