Skip to content

Commit

Permalink
Merge pull request #32 from dhruv-1001/feat/add-village-boundary
Browse files Browse the repository at this point in the history
feat: update setup script to download available village boundary data
  • Loading branch information
dhruv-1001 authored Feb 1, 2024
2 parents 496b889 + 138ffc8 commit 06b883e
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 18 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
node_modules

.idea
/server/geojson-data/*
.DS_store
server/.DS_Store
/server/geojson-data/indian_village_boundaries
/server/geojson-data/*json
1 change: 1 addition & 0 deletions server/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const swaggerApp = express();
swagger(swaggerApp);

const GeoLocationLevel = {
VILLAGE: 'VILLAGE',
SUBDISTRICT: 'SUBDISTRICT',
DISTRICT: 'DISTRICT',
STATE: 'STATE'
Expand Down
60 changes: 44 additions & 16 deletions server/scripts/parse.geojson.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,64 @@ console.log('Parsing INDIA_STATE');
const INDIA_STATE = JSON.parse(fs.readFileSync(`${geoJsonFilesPath}/INDIA_STATE.geojson`, 'utf8'));
featuresLength = INDIA_STATE.features.length;
for (let i = 0; i < featuresLength; i++) {
const locationProperty = INDIA_STATE.features[i].properties;
INDIA_STATE.features[i].properties = {
stname: locationProperty.STNAME,
stcode11: locationProperty.STCODE11,
levelLocationName: locationProperty.STNAME,
...locationProperty
}
const locationProperty = INDIA_STATE.features[i].properties;
INDIA_STATE.features[i].properties = {
stname: locationProperty.STNAME,
levelLocationName: locationProperty.STNAME,
...locationProperty
}
}
fs.writeFileSync(`${geoJsonFilesPath}/INDIA_STATE.geojson`, JSON.stringify(INDIA_STATE));

console.log('Parsing INDIA_DISTRICT');
const INDIA_DISTRICT = JSON.parse(fs.readFileSync(`${geoJsonFilesPath}/INDIA_DISTRICT.geojson`, 'utf8'));
featuresLength = INDIA_DISTRICT.features.length;
for (let i = 0; i < featuresLength; i++) {
const locationProperty = INDIA_DISTRICT.features[i].properties;
INDIA_DISTRICT.features[i].properties = {
levelLocationName: locationProperty.dtname,
...locationProperty
}
const locationProperty = INDIA_DISTRICT.features[i].properties;
INDIA_DISTRICT.features[i].properties = {
levelLocationName: locationProperty.dtname,
...locationProperty
}
}
fs.writeFileSync(`${geoJsonFilesPath}/INDIA_DISTRICT.geojson`, JSON.stringify(INDIA_DISTRICT));

console.log('Parsing INDIA_SUBDISTRICT');
const INDIA_SUBDISTRICT = JSON.parse(fs.readFileSync(`${geoJsonFilesPath}/INDIA_SUBDISTRICT.geojson`, 'utf8'));
featuresLength = INDIA_SUBDISTRICT.features.length;
for (let i = 0; i < featuresLength; i++) {
const locationProperty = INDIA_SUBDISTRICT.features[i].properties;
INDIA_SUBDISTRICT.features[i].properties = {
levelLocationName: locationProperty.sdtname,
const locationProperty = INDIA_SUBDISTRICT.features[i].properties;
INDIA_SUBDISTRICT.features[i].properties = {
levelLocationName: locationProperty.sdtname,
...locationProperty
}
}
fs.writeFileSync(`${geoJsonFilesPath}/INDIA_SUBDISTRICT.geojson`, JSON.stringify(INDIA_SUBDISTRICT));


console.log('Parsing indian_village_boundaries geoJSONs')
var states = fs.readdirSync(`${geoJsonFilesPath}/indian_village_boundaries/`);

for (const state of states) {
console.log(`Parsing geoJSON(s) for state ${state}`)
var stateFiles = fs.readdirSync(`${geoJsonFilesPath}/indian_village_boundaries/${state}`)
for (const file of stateFiles) {
if (!file.endsWith(`.geojson`)) continue;
console.log(`Parsing ${file}`);
const villagesGeoJson = JSON.parse(fs.readFileSync(`${geoJsonFilesPath}/indian_village_boundaries/${state}/${file}`));
const geoJsonLength = villagesGeoJson.features.length;
for (let i = 0; i < geoJsonLength; i++) {
const locationProperty = villagesGeoJson.features[i].properties;
villagesGeoJson.features[i].properties = {
dtName: locationProperty.DISTRICT,
stName: locationProperty.STATE,
sdtName: locationProperty.SUB_DIST,
levelLocationName: locationProperty.NAME,
...locationProperty
}
}
fs.writeFileSync(`${geoJsonFilesPath}/indian_village_boundaries/${state}/${file}`, JSON.stringify(villagesGeoJson));
}
}
fs.writeFileSync(`${geoJsonFilesPath}/INDIA_SUBDISTRICT.geojson`, JSON.stringify(INDIA_SUBDISTRICT));

const masterLocationNamesJson = require(`${geoJsonFilesPath}/MASTER_LOCATION_NAMES.json`)
fs.writeFileSync(`${geoJsonFilesPath}/PARSED_MASTER_LOCATION_NAMES.json`, JSON.stringify(masterLocationNamesJson));
21 changes: 20 additions & 1 deletion setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,27 @@ curl -Lo INDIA_DISTRICT.geojson "https://github.com/datta07/INDIAN-SHAPEFILES/ra
curl -Lo INDIA_SUBDISTRICT.geojson "https://github.com/datta07/INDIAN-SHAPEFILES/raw/master/INDIA/INDIAN_SUB_DISTRICTS.geojson"
curl -Lo INDIA_STATE.geojson "https://github.com/datta07/INDIAN-SHAPEFILES/raw/master/INDIA/INDIA_STATES.geojson"

# This JSON contains state > districts > subDistricts > villages for all states
curl -Lo MASTER_LOCATION_NAMES.json "https://github.com/pranshumaheshwari/indian-cities-and-villages/raw/master/data.json"

# village boundary
git clone https://github.com/datameet/indian_village_boundaries.git
# indian_village_boundaries repo cleanup
cd indian_village_boundaries
rm -rf .git docs website CONTRIBUTING.md LICENSE README.md
# Renaming states dir
mv ./br ./bihar
mv ./ga ./goa
mv ./gj ./gujarat
mv ./ka ./karnataka
mv ./kl ./kerala
mv ./mh ./maharashtra
mv ./or ./odisha
mv ./rj ./rajasthan
mv ./sk ./sikkim

# Changing PWD back to project root
cd - &> /dev/null
cd ../../..

# Updating geoJSON files through script to make them usable in server
cd server/scripts
Expand Down

0 comments on commit 06b883e

Please sign in to comment.