-
Hey team. Impressed with Mosaic so far, very snappy and generally easy to work with. I'm trying to render a map of the United States and I'm running into an issue. My goal is to render the map and overlay a dot mark showing the location of all the state capitals. I started out basing my code on the earthquakes example here: https://uwdata.github.io/mosaic/examples/earthquakes.html See error and code snippets below. GeoJSON and state capitals data files attached. Additional connection management code not shown here is taken from the Mosaic DuckDB-Wasm integration example project. Runtime environment is Chrome Version 120.0.6099.234. Dependencies:
Any help would be appreciated. Error
Snippet from Throwing Code
Component Codeimport { useState, useRef, useEffect } from 'react'
import * as vg from '@uwdata/vgplot';
import * as topojson from 'topojson-client'
export default function JobsByZipMap () {
const [vizElement, setVizElement] = useState<object | null>(null)
async function buildViz() {
try {
const capitalsJSON = await fetch('data/us_state_capitals.json')
.then(response => response.json())
await vg.coordinator().exec(
vg.loadObjects('capitals', capitalsJSON)
)
// confirming we have data in the database
const citiesStates = await vg.coordinator().query(
'select * from capitals',
{ type: 'json' }
)
console.info(citiesStates.map(row => { return row.toJSON() }))
console.info('query complete')
} catch (e) {
console.error('query failed', e)
throw e
}
const us = await fetch('data/us-counties-10m.json')
.then(result => result.json())
const nation = topojson.feature(us, us.objects.nation)
setVizElement(vg.plot(
vg.projectionType("albers-usa"),
vg.geo(
nation,
{ fill: "currentColor", fillOpacity: 0.2 }
),
vg.dot(
vg.from('capitals'),
{
x: 'lon',
y: 'lat',
stroke: 'red',
fill: 'red',
fillOpacity: '0.3'
}
),
)
)
}
useEffect(() => {
let ignore = false
// react render lifecycle bookkeeping
async function doBuildViz () {
if (!ignore) {
await buildViz()
}
}
doBuildViz()
return () => {
ignore = true
}
}, [])
const vizRef = useRef(null)
if (vizElement && vizRef && vizRef.current) {
// if we dont clear the ref's innerHTML, we end up with two instances of the viz
// likely due to our useEffect firing twice
vizRef.current.innerHTML = ''
vizRef.current.appendChild(vizElement)
}
return <div ref={vizRef}></div>
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
Hmm... maybe try replacing this const nation = topojson.feature(us, us.objects.nation) with this const nation = topojson.feature(us, us.objects.nation).features; vgplot doesn't currently recognize GeoJSON objects as input data, but should properly handle an |
Beta Was this translation helpful? Give feedback.
Hmm... maybe try replacing this
with this
vgplot doesn't currently recognize GeoJSON objects as input data, but should properly handle an
Array
containing individual GeoJSON features.