VegaLite.jl icon indicating copy to clipboard operation
VegaLite.jl copied to clipboard

cannot compute shape centroids for map labels

Open mthelm85 opened this issue 6 years ago • 5 comments

I'm unable to implement the geoCentroid() function that is shown in the Vega docs. This is needed to be able to place labels on maps.

mthelm85 avatar Feb 25 '19 12:02 mthelm85

The geoCentroid() function is a Vega command not a VegaLite command. As this package interfaces with VegaLite I would not expect that to work. I believe there is an experimental Vega interface but I could be wrong.

jordancluts avatar Feb 25 '19 16:02 jordancluts

I think in theory these should work in vega-lite as well: vega-lite specs are compiled into vega specs under the hood, and I would have assumed that one can therefore use the same functions in expressions. @mthelm85 could you post the code you are trying to run? Another good debug technique is to export the spec you are creating to JSON (p |> save("foo.vegalite")) and then try whether it works in the vega-lite online editor.

davidanthoff avatar Feb 25 '19 16:02 davidanthoff

I have tried to implement this code but could not figure out how to do it - even to reproduce the bug it reports.

johannspies avatar Mar 05 '19 06:03 johannspies

I want to plot the names of the countries in each country in the map below created by the following code code (I have renamed the uploaded files by appending .txt to be able to upload it): africa.topopjson.txt africa_output.csv.txt

using VegaLite #, VegaDatasets
using Queryverse
africa_output= "africa_output.csv"
africa = joinpath(".", "africa.topojson")
@vlplot(width=800, height=600) + 
@vlplot(
    mark={ 
        :geoshape,
    stroke=:black
    },
    data={
        url=africa,
        format={
           typ=:topojson,
            feature=:africa
        }
    },
    transform=[{
        lookup="properties.ISO_A3",
        from={
            data={
                url=africa_output,
                format={
                    typ=:csv
                }
            },
            key=:iso3166_12,
            fields=["count"]
        }
           }],
    color={
        "count:q",
        scale={domain=[0, 250000], scheme=:blues},
        legend={title="Output"}
    },
    projection={
        typ=:naturalEarth1
    }
)

deepinscreenshot_select-area_20190305090742

johannspies avatar Mar 05 '19 07:03 johannspies

It's a bit clunky, but this may solve the issue:

using VegaLite #, VegaDatasets
# using Queryverse
africa_output= "africa_output.csv"
africa = joinpath(".", "africa.topojson")
@vlplot(
    width=800, height=600, 
    projection={typ=:naturalEarth1}, 
    data={
        url=africa,
        format={
           typ=:topojson,
            feature=:africa
        }
    },
    transform=[
        {
            lookup="properties.ISO_A3",
            from={
                data={
                    url=africa_output,
                    format={
                        typ=:csv
                    }
                },
                key=:iso3166_12,
                fields=["count", "official_3"]
            }
        },
        {
            calculate="geoCentroid(null, datum)",
            as="centroid"
        },
        {
            calculate="datum.centroid[0]",
            as="centroidx"
        },
        {
            calculate="datum.centroid[1]",
            as="centroidy"
        }
        ]) + 
@vlplot(color={
            "count:q",
            scale={domain=[0, 250000], scheme=:blues},
            legend={title="Output"}
        },

        mark={ 
            :geoshape,
        stroke=:black
        }) +
@vlplot(mark=:text , text="official_3", longitude=:centroidx, latitude=:centroidy)

image

It looks like a more elegant solution may be in the pipeline for vega-lite, but it also doesn't look like it's a particularly high priority. Will this work for now?

mcmcgrath13 avatar Jul 31 '19 02:07 mcmcgrath13