-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
74,174 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
const axios = require('axios'); | ||
const fs = require("fs"); | ||
|
||
const DC_REST_URL = "https://api.datacommons.org/"; | ||
|
||
async function generateStateToCensusTracts() { | ||
let stateToCensusTracts = {}; | ||
|
||
try { | ||
let response = await axios.post(DC_REST_URL + "node/places-in", { | ||
dcids: "country/USA", | ||
placeType: "State" | ||
}); | ||
let payload = JSON.parse(response.data.payload); | ||
const stateIds = payload.map(x => x.place); | ||
|
||
let stateAbbrev = ""; | ||
let censusTractIds = []; | ||
for (const stateId of stateIds) { | ||
response = await axios.post(DC_REST_URL + "node/property-values", { | ||
dcids: [stateId], | ||
property: "fips52AlphaCode", | ||
}); | ||
payload = JSON.parse(response.data.payload); | ||
stateAbbrev = payload[stateId]["out"][0]["value"]; | ||
|
||
response = await axios.post(DC_REST_URL + "node/places-in", { | ||
dcids: stateId, | ||
placeType: "CensusTract", | ||
}); | ||
payload = JSON.parse(response.data.payload); | ||
censusTractIds = payload.map(x => x.place.substring(6)); | ||
|
||
stateToCensusTracts[stateAbbrev] = censusTractIds; | ||
} | ||
|
||
fs.writeFile("output/stateToCensusTracts.json", JSON.stringify(stateToCensusTracts, null, 2), (err) => { | ||
if (err) throw err; | ||
console.log("State to census tracts file generation complete"); | ||
}); | ||
} catch(err) { | ||
console.error(err); | ||
} | ||
} | ||
generateStateToCensusTracts(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.