Skip to content

Commit

Permalink
feat: add script to make geoJSON files generic
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruv-1001 committed Jan 22, 2024
1 parent 76c0850 commit 24db96d
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 18 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ node_modules

.idea
/server/geojson-data/*
.DS_store
server/.DS_Store
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ Get geolocation for given `lat` & `lon`
curl https://geoip.samagra.io/georev?lat=28.7041&lon=77.1025
```

Get polygon centroid for given `STATE/DISTRICT/SUBDISTRICT` with `zone` query
Get polygon centroid for given `STATE/DISTRICT/SUBDISTRICT` with some query
```shell
curl https://geoip.samagra.io/location/:DISTRICT/centroid?zone=lucknow
curl https://geoip.samagra.io/location/DISTRICT/centroid?query=lucknow
```

#### Notes
Expand Down
24 changes: 12 additions & 12 deletions server/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,35 +188,35 @@ export const app = new Router()
error: `Unsupported GeoLocation Level: ${locationLevel}`
}, { status: 400});
}
let zone = url.searchParams.get('zone');
if (!zone) {
let query = url.searchParams.get('query');
if (!query) {
return Response.json({
status: 'fail',
error: `No ${locationLevel} zone query found`
error: `No ${locationLevel} query found`
}, { status: 400 });
}
let zoneFeature;
let queryFeature;
for (const feature of geoJsonFiles[`${config.country}_${locationLevel}`].features) {
if (feature.properties.dtname.toLowerCase() === zone.toLowerCase()) {
zoneFeature = feature;
if (feature.properties.levelLocationName.toLowerCase() === query.toLowerCase()) {
queryFeature = feature;
}
}
if (!zoneFeature) {
if (!queryFeature) {
return Response.json({
status: 'fail',
error: `No ${locationLevel} found with name: ${zone}`
error: `No ${locationLevel} found with name: ${query}`
}, { status: 404 });
}
let polygonFeature;
if (zoneFeature.geometry.type === 'Polygon') {
polygonFeature = turf.polygon(zoneFeature.geometry.coordinates);
if (queryFeature.geometry.type === 'Polygon') {
polygonFeature = turf.polygon(queryFeature.geometry.coordinates);
} else {
polygonFeature = turf.multiPolygon(zoneFeature.geometry.coordinates);
polygonFeature = turf.multiPolygon(queryFeature.geometry.coordinates);
}
const centroid = turf.centroid(polygonFeature);
const longitude = centroid.geometry.coordinates[0];
const latitude = centroid.geometry.coordinates[1];
return Response.json(formatCentroidResponse(zoneFeature.properties, latitude, longitude), { status : 200 })
return Response.json(formatCentroidResponse(queryFeature.properties, latitude, longitude), { status : 200 })
} catch (error) {
return Response.json({
status: 'fail',
Expand Down
43 changes: 43 additions & 0 deletions server/scripts/parse.geojson.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import * as fs from 'fs';


const geoJsonFilesPath = `${import.meta.dir}/../geojson-data`;
let featuresLength;

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
}
}
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
}
}
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,
...locationProperty
}
}
fs.writeFileSync(`${geoJsonFilesPath}/INDIA_SUBDISTRICT.geojson`, JSON.stringify(INDIA_SUBDISTRICT));
8 changes: 4 additions & 4 deletions server/spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -315,14 +315,14 @@ paths:

/location/{locationlevel}/centroid:
get:
description: 'Return pylygon centroid coordinates & state for given district in query'
description: 'Return polygon centroid coordinates for given query from sub-district, district or state level'
parameters:
- name: locationlevel
in: path
schema:
type: string
required: true
- name: zone
- name: query
in: query
schema:
type: string
Expand All @@ -335,6 +335,6 @@ paths:
schema:
$ref: '#/components/schemas/CentroidReponseObject'
'400':
description: District not found in query
description: No query found
'404':
description: District Not Found in geojson
description: Query not Found in geojson
7 changes: 7 additions & 0 deletions setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,12 @@ 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"

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

# Updating geoJSON files through script to make them usable in server
cd server/scripts
bun parse.geojson.js

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

0 comments on commit 24db96d

Please sign in to comment.