Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tp.Topology.to_json(pretty=True) doesn't handle None correctly. (Doens't convert None to null) #149

Closed
yizongk opened this issue Mar 29, 2022 · 1 comment

Comments

@yizongk
Copy link
Contributor

yizongk commented Mar 29, 2022

Hi,

A Geopanda dataframe has None in one of its column, and then is converted to topology . When you want to export it with Topology.to_json(pretty=True), with pretty, you will notice that it doesn't convert any Nones to nulls.

Check this example (Notice "end_date": None)

import geopandas as gpd
import topojson as tp

gdf = gpd.GeoDataFrame.from_features({
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "properties": {
                "end_date": None,
                "pjct_name": "Central Park Drives Closures",
                "shape_leng": "24290.821126",
                "sip_year": "2015"
            },
            "geometry": {
                "type": "MultiLineString",
                "coordinates": [
                    [
                        [
                            -73.95894908875633,
                            40.78413830809674
                        ],
                        [
                            -73.95888401858302,
                            40.78422633413453
                        ]
                    ],
                ]
            }
        }
    ]
})

print(tp.Topology(gdf, prequantize=False).to_json(pretty=True))

You will see this output:

{
    "type": "Topology",
    "objects": {
        "data": {
            "geometries": [
                {
                    "properties": {
                        "end_date": None,
                        "pjct_name": "Central Park Drives Closures",
                        "shape_leng": "24290.821126",
                        "sip_year": "2015"
                    },
                    "type": "MultiLineString",
                    "arcs": [[0]]
                }
            ],
            "type": "GeometryCollection"
        }
    },
    "bbox": [-73.95894908875633, 40.78413830809674, -73.95888401858302, 40.78422633413453],
    "arcs": [
        [[-73.95894908875633, 40.78413830809674], [-73.95888401858302, 40.78422633413453]]
    ]
}

Under "properties" -> "end_date", you can see it's set to None, which is not correct JSON syntax. It should be null.

to_json() without pretty=True will output the correct format with null

@yizongk yizongk changed the title tp.Topology.to_json() doesn't handle None correctly. (Doens't convert None to null) tp.Topology.to_json(pretty=True) doesn't handle None correctly. (Doens't convert None to null) Mar 29, 2022
@yizongk
Copy link
Contributor Author

yizongk commented Mar 29, 2022

I think the issues stems from utils.py

topojson/topojson/utils.py

Lines 308 to 315 in 1c84fb4

def basictype2str(obj):
if isinstance(obj, str):
strobj = '"' + str(obj) + '"'
elif isinstance(obj, bool):
strobj = {True: "true", False: "false"}[obj]
else:
strobj = str(obj)
return strobj

I think a fix would be to put a check to see if obj is None, and then convert it to 'null', like:

# convert None to 'null' since None is not a valid syntax in JSON
elif obj is None:
    strobj = 'null'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant