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

add documentation how to load a local topojson file #1316

Closed
djouallah opened this issue Jan 31, 2019 · 12 comments
Closed

add documentation how to load a local topojson file #1316

djouallah opened this issue Jan 31, 2019 · 12 comments

Comments

@djouallah
Copy link

is this pseudo code supported

import pandas as pd
import altair as alt

counties = alt.topo_feature('https://raw.githubusercontent.com/djouallah/R_leaflet_MAP/master/foundation.json', 'id')

map =alt.Chart(counties).mark_geoshape().encode(
    color='id:Q'
                                                 )
map

all the documentation is using "data.world_110m.url", how about local topojson or geojson files ?

@jakevdp
Copy link
Collaborator

jakevdp commented Jan 31, 2019

The file needs to be accessible via HTTP from the renderer. In Juypyter notebook, for example, you can put the file in the same directory as the notebook and then pass the filename to alt.topo_feature.

@djouallah
Copy link
Author

I tried this , but not working, I save the json file in the same directory

import pandas as pd
import altair as alt

counties = alt.topo_feature('foundation.json', 'id')

map =alt.Chart(counties).mark_geoshape().encode(
    color='id:Q'
                                                 )
map

@jakevdp
Copy link
Collaborator

jakevdp commented Jan 31, 2019

What frontend are you using? This won't work, for example, in Binder, but it will work in a local JupyterLab frontend.

@djouallah
Copy link
Author

I am using a local jupyter lab, and it produces empty chart

@jakevdp
Copy link
Collaborator

jakevdp commented Jan 31, 2019

Does it work if you use a local data file for a basic dataset? i.e. this works for me in a local JupyterLab:

import altair as alt
from vega_datasets import data

cars = data.cars()
filename = 'cars.json'
cars.to_json(filename, orient='records')

alt.Chart(filename).mark_point().encode(
    x='Horsepower:Q',
    y='Miles_per_Gallon:Q',
    color='Origin:N'
)

@djouallah
Copy link
Author

yes that code works

@jakevdp
Copy link
Collaborator

jakevdp commented Jan 31, 2019

In that case, I suspect the issue is your topojson file is not correctly formatted.

To be honest, I don't know much about topojson and have not used this part of the library, so I'm not going to be much help in debugging it. I'd start by comparing the format of your topojson file with the files used in the examples, and see if there are any glaring inconsistencies.

If you figure anything out and would like to add hints to the documentation, that would be greatly appreciated.

@djouallah
Copy link
Author

yeah, I found the issue, the second argument in alt.topo_feature has to be the name of the objects not the id, in the attached example "objects":{"foundation":{

import altair as alt
floor_Plan = alt.topo_feature('https://raw.githubusercontent.com/djouallah/R_leaflet_MAP/master/foundation.json', 'foundation')
alt.Chart(floor_Plan).mark_geoshape().encode(
color='properties.id:N',
tooltip=['properties.id:N'])

for some reason, we need to add "properties" to the field name ?

thanks for the help

@mattijn
Copy link
Contributor

mattijn commented Feb 2, 2019

I know some of TopoJSON, as 'm working on a Python package regarding this extension of GeoJSON (
https://github.com/mattijn/topojson)*. Its work in progress. but potentially to be used as an interchange format between geographical packages in the Python ecosystem (eg. geopandas and altair).

Without explaining the format, you are dealing with nested objects. So in the alt.topo_feature(url, feature) you have to define the feature or layer that is present within the objects in your TopoJSON file.

This layer or feature (in your case named foundation) contains often multiple geometries, where each geometry refers to certain top-level stored geometric segments or so-called arcs. Next to the geometry, you have to store some attributes of that geometry, which is stored in another nested object properties.

So in order to access these attribute information you have to add properties to enter this nested object.

{
  "type": "Topology",
  "arcs": [[[641, 1092], [0, 116], [89, 0], [0, -116], [-89, 0]]],
  "objects": {
    "foundation": {
      "type": "GeometryCollection",
      "geometries": [
        {
          "arcs": [[0]],
          "type": "Polygon",
          "properties": {
            "id": 22,
            "width": 0.000558,
            "height": 0.000719,
            "area": 0,
            "perimeter": 0.002555,
            "type": "foundation"
          }
        }
      ]
    }
  }
}

Your issue have been discussed before on the Vega-repo: vega/vega#1319
And more work has also been done in: https://iliatimofeev.github.io/gpdvega/

@djouallah
Copy link
Author

@mattijn thanks for the explanation, I appreciate if you have a look at #1322

@kannes
Copy link

kannes commented May 16, 2019

Dear future reader:

  • if you use Jupyter Notebook
  • if you try to load data from a non-HTTPS url but HTTP

This will fail due to CORS for reasons I cannot imagine.
There will be no warnings nor errors messages visibly shown anywhere (unless you consider the browser console as a valid place to look at).
You will just get a blank chart.

So, make sure you check the browser console when you do anything in Altair in Jupyter Notebook and make sure you do not run into CORS errors.

@Deepakpratap
Copy link

@djouallah your problem literally solved my problem.I wanted to plot for the country India...It is sorted now

`
import altair as alt

url = "https://raw.githubusercontent.com/deldersveld/topojson/master/countries/india/india-states.json"

source = alt.topo_feature(url, "IND_adm1")

alt.Chart(source).mark_geoshape().encode(
tooltip='properties.NAME_1:N'
)
`

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

No branches or pull requests

6 participants