Search code examples
jsond3.jstopojson

Return State ID from Containing County Event


I have a topojson map that has both county and state data in json format. There are no actual names for the states, but the states do each have a unique id. I would like to find a way to return the id of a state if the user clicks on one of its containing counties.

The trouble is I cannot seem to be able to access the state ids in the json when I set up my click event listener. I have sliced and diced the json data every which way, but I keep going in circles. Is it possible to have asymmetric information within the json file? I feel like the state ids are in a black box when working with the containing counties.

Let me know if anything comes to mind. I have a fully functional minimalist example here, where I am trying to return a console log of the state id based on user click.

Note: I would prefer to avoid point in polygon solutions for complexity reasons.


Solution

  • Seems your json file ships with FIPS county code. That means that first two digits of county code are in fact the state code. So Math.floor(county.id / 1000) is what you‘re looking for in the end.