# How to make vegalite read my topojson file?

**URL:** https://discourse.julialang.org/t/how-to-make-vegalite-read-my-topojson-file/34546
**Category:** General Usage
**Tags:** first-steps, vegalite
**Created:** [February 12, 2020, 10:06pm UTC](https://discourse.julialang.org/t/how-to-make-vegalite-read-my-topojson-file/34546 "2020-02-12T22:06:07Z")
**Posts on this page:** 19
**Page:** 1

<div class="post-metadata">

### Author: ![Aizzaac](https://avatars.discourse-cdn.com/v4/letter/a/aca169/32.png) [@Aizzaac](https://discourse.julialang.org/u/Aizzaac)
#### Post date: [February 12, 2020, 10:06pm UTC](https://discourse.julialang.org/t/how-to-make-vegalite-read-my-topojson-file/34546/1 "2020-02-12T22:06:07Z")

</div>

I have a topojson file located in this path: `"/home/juliana/roc/Data/Topojson/disa_ONT_region"`  
I want to make a choropleth map, but I do not know how to call that file into my code.

I know it has to go in “values”  
I have tried:

Point a) is related to this: [GitHub - rofinn/FilePaths.jl: A type based approach to working with filesystem paths in julia](https://github.com/rofinn/FilePaths.jl)

a) file=p"/home/juliana/roc/Data/Topojson/disa\_ONT\_region"

b) file=normpath(string(@ **DIR** , “/Data/Topojson/”, “disa\_ONT\_region”))

This is my code:

```
@vlplot(width=800, height=600) +
@vlplot(
    mark={
        :geoshape
},
data={
    values=file,
    format={
        typ=:topojson,
        feature=:disa_ONT_region
    }
},
transform=[{
    lookup=:featureId,
    from={
        data=df_splunk,
        key=:featureId,
        fields=["count"]
    }
}],
color={
    "rate:q",
    scale={scheme=:reds},
    legend={title="IP Rate - BST"}
},
projection={
    typ=:albersUsa
}
)

```

---

<div class="post-metadata">

### Author: ![ImreSamu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/imresamu/32/20677_2.png) [@ImreSamu](https://discourse.julialang.org/u/ImreSamu)
#### Post date: [February 12, 2020, 10:32pm UTC](https://discourse.julialang.org/t/how-to-make-vegalite-read-my-topojson-file/34546/2 "2020-02-12T22:32:02Z")

</div>

adapting this example - is helping?

- [Maps (Geographic Displays) · VegaLite.jl](https://www.queryverse.org/VegaLite.jl/stable/examples/examples_maps/#Choropleth-of-unemployment-rate-per-county-1)

---

<div class="post-metadata">

### Author: ![Aizzaac](https://avatars.discourse-cdn.com/v4/letter/a/aca169/32.png) [@Aizzaac](https://discourse.julialang.org/u/Aizzaac)
#### Post date: [February 12, 2020, 10:59pm UTC](https://discourse.julialang.org/t/how-to-make-vegalite-read-my-topojson-file/34546/3 "2020-02-12T22:59:38Z")

</div>

I have already done that. I just want to upload a .topojson file from my computer instead of using a VEGAdataset.

---

<div class="post-metadata">

### Author: ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)
#### Post date: [February 13, 2020, 12:01am UTC](https://discourse.julialang.org/t/how-to-make-vegalite-read-my-topojson-file/34546/4 "2020-02-13T00:01:48Z")

</div>

What does `isfile(file)` give you?

---

<div class="post-metadata">

### Author: ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)
#### Post date: [February 13, 2020, 12:03am UTC](https://discourse.julialang.org/t/how-to-make-vegalite-read-my-topojson-file/34546/5 "2020-02-13T00:03:34Z")

</div>

Additionally, when your code isn’t working, please post the error message (not a screenshot, with backticks) to help us understand what is going on.

---

<div class="post-metadata">

### Author: ![davidanthoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidanthoff/32/223493_2.png) [@davidanthoff](https://discourse.julialang.org/u/davidanthoff)
#### Post date: [February 13, 2020, 1:16am UTC](https://discourse.julialang.org/t/how-to-make-vegalite-read-my-topojson-file/34546/6 "2020-02-13T01:16:55Z")

</div>

There are two different ways in which you can load data: 1) you can create a `Path` object from the [FilePaths.jl](https://github.com/rofinn/FilePaths.jl) package that points to your file and put that into your spec, or 2) you can pass the values themselves.

In the case of 1) you are not loading the values yourself, but you let vega/vega-lite load the data from disc, i.e. you just put a pointer to the file into the figure. In theory this is great, but in practice we have a problem that this doesn’t work for various display front-ends (it doesn’t work in VS Code, ElectronDisplay and the default browser viewer, not sure about Jupyter). It _does_ work if you save such a figure to disc with `save`.

In the case of 2) you need to load the data yourself into memory in Julia, and then pass the data itself to vega/vega-lite. This should always work.

In the case of 1) we don’t have to worry about different file formats, vega is going to figure it out.

In the case of 2) we need to differentiate a bit between tabular and other data. For tabular data anything that complies with the [TableTraits.jl](https://github.com/queryverse/TableTraits.jl) interface can be passed, which is pretty much everything, so that is easy. For non-tabular data, for example a topjson, you should ideally use [JSON.jl](https://github.com/JuliaIO/JSON.jl) to read the file content and pass it to [VegaLite.jl](https://github.com/queryverse/VegaLite.jl). The code might look like this:

```julia
data={
    values=JSON.parsefile(filename),
    format={
        typ=:topojson,
        feature=:disa_ONT_region
    }
}

```

Let me know if this works!

---

<div class="post-metadata">

### Author: ![Aizzaac](https://avatars.discourse-cdn.com/v4/letter/a/aca169/32.png) [@Aizzaac](https://discourse.julialang.org/u/Aizzaac)
#### Post date: [February 13, 2020, 2:29pm UTC](https://discourse.julialang.org/t/how-to-make-vegalite-read-my-topojson-file/34546/7 "2020-02-13T14:29:33Z")

</div>

Will a dataframe be in the category of a tabular data?  
I have my .shp file also as a dataframe. But I was not able to make a choropleth map. That is why I changed to Vegalite.jl.

By the way this is how my data looks like in a dataframe. It can be seen in 2 ways:

A. The fourth and fifth column do not appear

 ![image](https://global.discourse-cdn.com/julialang/original/3X/8/7/87e414512d30bd3336b6a335c9ce2261c50f19a9.png)

B.  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/9/b/9bdfbcb4ecf5f469b2dfa3ff843abace1d893323.png)

---

<div class="post-metadata">

### Author: ![Aizzaac](https://avatars.discourse-cdn.com/v4/letter/a/aca169/32.png) [@Aizzaac](https://discourse.julialang.org/u/Aizzaac)
#### Post date: [February 13, 2020, 2:45pm UTC](https://discourse.julialang.org/t/how-to-make-vegalite-read-my-topojson-file/34546/8 "2020-02-13T14:45:42Z")

</div>

false

---

<div class="post-metadata">

### Author: ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)
#### Post date: [February 13, 2020, 2:57pm UTC](https://discourse.julialang.org/t/how-to-make-vegalite-read-my-topojson-file/34546/9 "2020-02-13T14:57:05Z")

</div>

Please stop posting screenshots, they make discourse not searchable for future people looking for help.

Your file isn’t in the place where you say it is, or your use of `normpath` is not correct. This is definitely the problem. It’s not to do with vegalite. You need to make sure `isfile` returns `true` (meaning the path leads to an actual file on your computer).

---

<div class="post-metadata">

### Author: ![Aizzaac](https://avatars.discourse-cdn.com/v4/letter/a/aca169/32.png) [@Aizzaac](https://discourse.julialang.org/u/Aizzaac)
#### Post date: [February 13, 2020, 3:19pm UTC](https://discourse.julialang.org/t/how-to-make-vegalite-read-my-topojson-file/34546/10 "2020-02-13T15:19:19Z")

</div>

It worked!

I installed the JSON.jl and then I used your code.

I have a question… so if I had a dataframe I will need to “parse” it before sending it to VegaLite, right?

---

<div class="post-metadata">

### Author: ![Aizzaac](https://avatars.discourse-cdn.com/v4/letter/a/aca169/32.png) [@Aizzaac](https://discourse.julialang.org/u/Aizzaac)
#### Post date: [February 13, 2020, 3:39pm UTC](https://discourse.julialang.org/t/how-to-make-vegalite-read-my-topojson-file/34546/11 "2020-02-13T15:39:22Z")

</div>

What does this command do?

```
push!(LOAD_PATH,string(@ __DIR__ ,"/Data/Topojson/"))

```

It gives mes this output:

```
 "@"                               
 "@v#.#"                           
 "@stdlib"                         
 "/home/juliana/roc/Data/Topojson/"

```

---

<div class="post-metadata">

### Author: ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)
#### Post date: [February 13, 2020, 3:43pm UTC](https://discourse.julialang.org/t/how-to-make-vegalite-read-my-topojson-file/34546/12 "2020-02-13T15:43:14Z")

</div>

`LOAD_PATH` is for where julia looks for julia packages, _not_ files for import. You shouldn’t be using `LOAD_PATH` to import files.

Maybe try just working with the global path as a big string instead of messing with `@ __DIR__ ` for now.

---

<div class="post-metadata">

### Author: ![neelsmith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/neelsmith/32/22638_2.png) [@neelsmith](https://discourse.julialang.org/u/neelsmith)
#### Post date: [March 7, 2021, 7:50am UTC](https://discourse.julialang.org/t/how-to-make-vegalite-read-my-topojson-file/34546/13 "2021-03-07T07:50:43Z")

</div>

I’m having a similar problem – I can plot data from Vegalite’s datasets, but fail trying to use topojson downloaded from [https://github.com/topojson/world-atlas](https://github.com/topojson/world-atlas)

I’m `using VegaLite, VegaDatasets`, have parsed a valid topojson file with

`countries50m = JSON.parsefile(DOWNLOADEDFILE)`

Here’s my failing code:

```julia
function plottopo(w = 900, h = 500)
    @vlplot(width=w, height=h) +
    @vlplot(
        projection={
			type=:naturalEarth1
		},
        mark={
            :geoshape,
            fill=:transparent,
            stroke=:gray
        },
        data={
            values=countries50m,
            format={
                type=:topojson,
                features=:land
            }
        }
    ) 
end

```

The error message begins

```julia
ERROR: THIS SHOULDN'T HAPPEN Dict{String,Any}("objects" => Dict{String,Any}("land" => Dict{String,Any}("geometries" => Any[Dict{String,Any}("arcs" => Any[Any[Any[0]], Any[Any[1]], 
....

```

and continues with the entire geometry of the world.

Comparing it to the data packaged in VegaliteDatasets, I notice this:

```julia
julia> dataset("world-110m").data["objects"]["land"]
OrderedCollections.OrderedDict{String,Any} with 2 entries:
  "type" => "MultiPolygon"
  "arcs" => Any[Any[Any[0]], Any[Any[1]], Any[Any[2]], Any[Any[3]], Any[Any[4]]…

julia> countries50m["objects"]["land"]
Dict{String,Any} with 2 entries:
  "geometries" => Any[Dict{String,Any}("arcs"=>Any[Any[Any[853, 1846, 1480, 184…
  "type" => "GeometryCollection"

```

Is it significant that the type of the downloaded topojson is `"GeometryCollection"` and the Vegalite data is `"MultiPolygon"`?

Any help appreciated!

---

<div class="post-metadata">

### Author: ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)
#### Post date: [March 7, 2021, 6:57pm UTC](https://discourse.julialang.org/t/how-to-make-vegalite-read-my-topojson-file/34546/14 "2021-03-07T18:57:15Z")

</div>

It’s not so clear from your code what the final goal is, so I hope that these steps to overcome the error help for your next steps.

I downloaded the file [https://cdn.jsdelivr.net/npm/world-atlas@2/countries-50m.json](https://cdn.jsdelivr.net/npm/world-atlas@2/countries-50m.json) to `c:\Temp`:

```julia
using VegaLite, VegaDatasets, JSON
cd("c:\\Temp")
countries50m = JSON.parsefile("c:\\Temp\\countries-50m.json")
d=VegaDatasets.VegaJSONDataset(countries50m,"c:\\Temp\\countries-50m.json")
@vlplot(
    :geoshape,
    width=500, height=300,
    data={
        values=d,
        format={
            type=:topojson,
            feature=:countries
        }
    },
    projection={
        type=:albersUsa
    },
)

```

![grafik](https://global.discourse-cdn.com/julialang/original/3X/3/f/3fdce259bab27b70b6205354419415a63726b968.png)

---

<div class="post-metadata">

### Author: ![neelsmith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/neelsmith/32/22638_2.png) [@neelsmith](https://discourse.julialang.org/u/neelsmith)
#### Post date: [March 7, 2021, 8:16pm UTC](https://discourse.julialang.org/t/how-to-make-vegalite-read-my-topojson-file/34546/15 "2021-03-07T20:16:10Z")

</div>

The goal was exactly what you’ve done – display the map. Thanks so much, this is perfect! (I missed the step of creating a VegaJSONDataset, and was trying to pass in the topojson directly)

(The motivation for a function hard coded with a reference to a data set was simply to hide some of the variables and have an easy function to display the layer in a Pluto notebook.)

---

<div class="post-metadata">

### Author: ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)
#### Post date: [March 7, 2021, 8:27pm UTC](https://discourse.julialang.org/t/how-to-make-vegalite-read-my-topojson-file/34546/16 "2021-03-07T20:27:48Z")

</div>

Provide the JSON data as parameter to the function, like:

```julia
function plottopo(data, w = 900, h = 500)
    ...
        data={
            values=data, ...

end

```

to avoid the need of global variables.  
(side not, off topic) 🙂

---

<div class="post-metadata">

### Author: ![neelsmith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/neelsmith/32/22638_2.png) [@neelsmith](https://discourse.julialang.org/u/neelsmith)
#### Post date: [March 7, 2021, 9:25pm UTC](https://discourse.julialang.org/t/how-to-make-vegalite-read-my-topojson-file/34546/17 "2021-03-07T21:25:09Z")

</div>

Motivation was specific to Pluto nb enviornment: I’ve got a couple of maps on the same page. This is just a snippet of the function that overlays dynamically filtered data based on criteria set in PlutoUI widgets. The plotting is actually specific to the data set, so not really generalizable.

But thanks for the concern. 🙂 I don’t actually randomly use global variables!

---

<div class="post-metadata">

### Author: ![alexfakos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alexfakos/32/209024_2.png) [@alexfakos](https://discourse.julialang.org/u/alexfakos)
#### Post date: [March 5, 2025, 2:37pm UTC](https://discourse.julialang.org/t/how-to-make-vegalite-read-my-topojson-file/34546/18 "2025-03-05T14:37:58Z")

</div>

Hi, I face the same problem but this fix doesn’t seem to work for me. Below are two functions. One loads the .json file from a URL and the other uses oheil’s approach to load a local file. But the local file method doesn’t work. Can you please help me out? It’s the first time I am using a json file and I cannot wrap my mind around it. Thanks!

```julia
import VegaLite
import VegaDatasets
import DataFrames as DF
import FilePathsBase
import URIs
import JSON

import CSV

function demo_fails()
    #jsonpath = URIs.URI(FilePathsBase.absolute(FilePathsBase.p"../input/mexicoHigh.json") )
    #jsonpath = FilePathsBase.absolute(FilePathsBase.p"../input/mexicoHigh.json") 
    jsonval = JSON.parsefile("../input/mexicoHigh.json")
    d=VegaDatasets.VegaJSONDataset(jsonval,"../input/mexicoHigh.json")

    mexico_geojson = "https://raw.githubusercontent.com/angelnmara/geojson/master/mexicoHigh.json"
    themap = VegaLite.@vlplot( width=640, height=360, projection={ type="mercator"},title="Mexico",
                              mark={type="geoshape", fill="lightgray", stroke="white"}, 
                              data={values=d, format={type="topojson", feature="State"} })
    out = themap
    VegaLite.save("../temp/test4.pdf",out)
end 
function demo_works()
    mexico_geojson = "https://raw.githubusercontent.com/angelnmara/geojson/master/mexicoHigh.json"
    themap = VegaLite.@vlplot( width=640, height=360, projection={ type="mercator"},title="Mexico",
                              mark={type="geoshape", fill="lightgray", stroke="white"},
                              data={url=mexico_geojson, format={type="topojson", feature="State"} })
    out = themap
    VegaLite.save("../temp/test4.pdf",out)
end

```

---

<div class="post-metadata">

### Author: ![alexfakos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alexfakos/32/209024_2.png) [@alexfakos](https://discourse.julialang.org/u/alexfakos)
#### Post date: [March 5, 2025, 3:39pm UTC](https://discourse.julialang.org/t/how-to-make-vegalite-read-my-topojson-file/34546/19 "2025-03-05T15:39:15Z")

</div>

The following works, but I don’t know why. If I use a path, I cannot specify the format?

```julia
import VegaLite
import VegaDatasets
import DataFrames as DF
import FilePathsBase
import URIs
import JSON

import CSV

function demo_map_path()
    #jsonpath = URIs.URI(FilePathsBase.absolute(FilePathsBase.p"../input/mexicoHigh.json") )
    #jsonval = JSON.parsefile("../input/mexicoHigh.json")
    #d=VegaDatasets.VegaJSONDataset(jsonval,"../input/mexicoHigh.json")
    jsonpath = FilePathsBase.absolute(FilePathsBase.p"../input/mexicoHigh.json")
    themap = VegaLite.@vlplot( width=640, height=360, projection={ type="mercator"},title="Mexico",
                              mark={type="geoshape", fill="lightgrey", stroke="white"},
                              data=jsonpath)
    out = themap
    VegaLite.save("../temp/test5.pdf",out)
end 

```
