You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I had to write code to fetch and unzip all the states' archives. I wanted the shapes and metadata about each. here's a look at what these blocks look like when opened in Google Earth Pro:
Here's the approach I'm taking to processing one state. I need one DBF, one SHX, and one SHP file:
const dbfFile = await loadFile(`${tempPath}/${sourceName}/${sourceName}.dbf`)
const shpFile = await loadFile(`${tempPath}/${sourceName}/${sourceName}.shp`)
const shxFile = await loadFile(`${tempPath}/${sourceName}/${sourceName}.shx`)
const dbfReader = await DbfReader.fromFile(dbfFile)
const shpReader = await ShapeReader.fromFile(shpFile, shxFile)
for (let i = 0; i < shpReader.recordCount; i++) {
const dbf = dbfReader.readRecord(i)
const geom = shpReader.readGeom(i)
const geoJson = geom.toGeoJson() as GeoJsonPolygon
const fields: any = {}
dbfReader.fields.forEach((f, j) => {
fields[f.name] = dbf[j]
})
// Your code for handling one census block goes here:
console.log(fields, geoJson.coordinates)
}
The only challenge I had was in creating the loadFile() function. I used your on fileMock.ts code as a starting point:
export const loadFile = async (filePath: string): Promise<FileMock> => {
const buf = await fs.promises.readFile(filePath)
const ab = new ArrayBuffer(buf.length)
const view = new Uint8Array(ab)
for (let i = 0; i < buf.length; ++i) {
view[i] = buf[i]
}
return new FileMock(ab)
}
class FileMock implements File {
private _buf: ArrayBuffer
constructor(buffer: ArrayBuffer) {
this._buf = buffer
}
lastModified: number
name: string
webkitRelativePath: string
size: number
type: string
bytes(): Promise<Uint8Array> {
throw new Error('Method not implemented.')
}
slice(start?: number, end?: number, contentType?: string): Blob {
throw new Error('Method not implemented.')
}
stream(): ReadableStream<Uint8Array> {
throw new Error('Method not implemented.')
}
text(): Promise<string> {
throw new Error('Method not implemented.')
}
public async arrayBuffer(): Promise<ArrayBuffer> {
return this._buf
}
}
No doubt I could have done this part in a more elegant way than I ultimately did. Consider adding handlers for file paths as strings if you decide to update this someday.
Thank you again for sharing!
The text was updated successfully, but these errors were encountered:
Thank you for this. It works very well for me. I'm using it presently to process the US Census Department's 2020 census block shapes for each state:
https://www.census.gov/cgi-bin/geo/shapefiles/index.php?year=2024&layergroup=Blocks+%282020%29
I had to write code to fetch and unzip all the states' archives. I wanted the shapes and metadata about each. here's a look at what these blocks look like when opened in Google Earth Pro:
Here's the approach I'm taking to processing one state. I need one DBF, one SHX, and one SHP file:
The only challenge I had was in creating the
loadFile()
function. I used your on fileMock.ts code as a starting point:No doubt I could have done this part in a more elegant way than I ultimately did. Consider adding handlers for file paths as strings if you decide to update this someday.
Thank you again for sharing!
The text was updated successfully, but these errors were encountered: