Package: pandas-geojson
FeaturesConvert DataFame with Lat Long Coordinates to GeoJSONWrite & Save GeoJSON filesOpen GeoJSON from file or urlFilter GeoJSON based on list criteriaGetting StartedInstall the latest version of pandas-geojson using pip.pip install pandas-geojsonConverting DataFrames to GeoJSONTo use this first function, you will need a csv file with latitude and lognitude coordinates. If you do not have one to use, there is a sample CSV in the Repo. Use pandas to read in your data.INPUTimport pandas as pd
data = pd.read_csv('Test.csv')
print(data.head())OUTPUTname        lat        long marker-symbol marker-color
0  Random  48.702076 -111.855280          star      #C91313
1  Random  46.768477 -111.903907          star      #C91313
2  Random  49.768477 -112.903907          star      #C91313
3  Random  45.768477 -110.903907          star      #C91313Definie your variable and call the to_geojson function. You will need to pass your dataframe object and specify the lat and long cordinate columns. You can also pass a list with the properties you want to include (essentially the meta data of the coordinates).INPUTfrom pandas_geojson import to_geojson
geo_json = to_geojson(df=data, lat='lat', lon='long',
                 properties=['name','marker-symbol','marker-color'])
print(geo_json)OUTPUT{'type': 'FeatureCollection', 'features': [{'type': 'Feature', 'properties': {'name': 'Random', 'marker-symbol': 'star', 'marker-color': '#C91313'}, 'geometry': {'type': 'Point', 'coordinates': [-111.85528049999999, 48.70207631]}}, {'type': 'Feature', 'properties': {'name': 'Random', 'marker-symbol': 'star', 'marker-color': '#C91313'}, 'geometry': {'type': 'Point', 'coordinates': [-111.90390740000001, 46.768476899999996]}}, {'type': 'Feature', 'properties': {'name': 'Random', 'marker-symbol': 'star', 'marker-color': '#C91313'}, 'geometry': {'type': 'Point', 'coordinates': [-112.90390740000001, 49.768476899999996]}}, {'type': 'Feature', 'properties': {'name': 'Random', 'marker-symbol': 'star', 'marker-color': '#C91313'}, 'geometry': {'type': 'Point', 'coordinates': [-110.90390740000001, 45.768476899999996]}}]}Opening GeoJSONThere are two functions for opening geojson files. The first function opens it from a URL, the second opens from files on your local computer.from pandas_geojson import read_geojson_url
geo_json = read_geojson_url('<URL>')ORfrom pandas_geojson import read_geojson
geo_json = read_geojson('<FILENAME>')Saving GeoJSON FilesThe save function has two required arguements. The first is the geojson object. The second is the filename. Adding indentation and formatting is optional, but is useful for saving geojson in an easy-to-read format.INPUTfrom pandas_geojson import write_geojson
write_geojson(geo_json, filename='random.geojson', indent=4)OUTPUT{
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "properties": {
                "name": "Randomize",
                "marker-symbol": "star",
                "marker-color": "#C91313"
            },
            "geometry": {
                "type": "Point",
                "coordinates": [
                    -111.8552805,
                    48.70207631
                ]
            }
        },
        {
            "type": "Feature",
            "properties": {
                "name": "Random",
                "marker-symbol": "star",
                "marker-color": "#C91313"
            },
            "geometry": {
                "type": "Point",
                "coordinates": [
                    -111.9039074,
                    46.7684769
                ]
            }
        },
        {
            "type": "Feature",
            "properties": {
                "name": "Random",
                "marker-symbol": "star",
                "marker-color": "#C91313"
            },
            "geometry": {
                "type": "Point",
                "coordinates": [
                    -112.9039074,
                    49.7684769
                ]
            }
        },
        {
            "type": "Feature",
            "properties": {
                "name": "Randomize",
                "marker-symbol": "star",
                "marker-color": "#C91313"
            },
            "geometry": {
                "type": "Point",
                "coordinates": [
                    -110.9039074,
                    45.7684769
                ]
            }
        }
    ]
}Filtering GeoJSONGeoJSON files can be large and complex. Filtering to what is important is a useful tool. To use this function you need to have a list of the items you want to filter to. Once you have that, load your geojson file using the read_geojson function shown above. Set you output variable name and then call the filter_geojson function. Pass your geojson object and filter list. Finally, you will need a property key to identify where to look for the items in your list. After running the code, you should get a new filtered geojson object.INPUTfrom pandas_geojson import read_geojson, filter_geojson
filter = ['Randomize']
geojson = read_geojson('random.geojson')
output = filter_geojson(geo_json=geojson, filter_list=filter, property_key='name')
print(output)OUTPUT{"type": "FeatureCollection", "features": [{"type": "Feature", "properties": {"name": "Randomize", "marker-symbol": "star", "marker-color": "#C91313"}, "geometry": {"type": "Point", "coordinates": [-111.8552805, 48.70207631]}}, {"type": "Feature", "properties": {"name": "Randomize", "marker-symbol": "star", "marker-color": "#C91313"}, "geometry": {"type": "Point", "coordinates": [-110.9039074, 45.7684769]}}]}