I have a set of coordinates retrieved as:
navigator.geolocation.getCurrentPosition(function(position) {
let coordinates: [number, number] = [position.coords.longitude, position.coords.latitude]
}
// e.g. [-70.1234567, 40.987654]
I want to use D3 to retrieve the corresponding county from a U.S. TopoJSON file as follows:
getGeoLocation(topology, coordinates) {
topojson.feature(topology, topology["objects"]["counties"])["features"].filter(function(polygon) {
polygon["geometry"]["coordinates"].forEach(coord => {
console.log(d3.polygonContains(coord, coordinates))
})
})
}
Doing this returns false for every county object however. I'm not sure if the best approach is to use d3.geoContains() or d3.polygonContains(), although online it seems there may be some issue related to MultiPolygon features in the TopoJSON (link here: https://cdn.jsdelivr.net/npm/us-atlas@3/counties-albers-10m.json).
How can I resolve this?
EDIT
Perhaps the issue is lack of projection into the geoPath() being used on the TopoJSON...? Although even if I don't project through and just compare to the basic JSON, the issue remains...
Some context on the map (built in Angular with TypeScript), where this.topology is the JSON import from the specified URL:
path: any = d3.geoPath()
this.svg = d3.select("#map")
.attr("preserveAspectRatio", "xMidYMid meet")
.attr("viewBox", "0 0 975 610")
.attr("height", "100%")
.attr("width", "100%")
this.g = this.svg.append("g")
.attr("id", "g")
this.counties = this.g.append("g")
.selectAll("path")
.data(topojson.feature(this.topology, this.topology["objects"]["counties"])["features"])
.join("path")
.attr("d", this.path)
.attr("class", "county")
.attr("id", function(d) {return d["id"]})