diff --git a/.gitignore b/.gitignore index 2a867de..4f47424 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,5 @@ node_modules .idea /server/geojson-data/* +.DS_store +server/.DS_Store diff --git a/readme.md b/readme.md index c8f2064..a88ab8b 100644 --- a/readme.md +++ b/readme.md @@ -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 diff --git a/server/app.js b/server/app.js index 1ecad1c..7f13953 100644 --- a/server/app.js +++ b/server/app.js @@ -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', diff --git a/server/scripts/parse.geojson.js b/server/scripts/parse.geojson.js new file mode 100644 index 0000000..f90110b --- /dev/null +++ b/server/scripts/parse.geojson.js @@ -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)); diff --git a/server/spec.yaml b/server/spec.yaml index f4f02d1..a45cd83 100644 --- a/server/spec.yaml +++ b/server/spec.yaml @@ -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 @@ -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 diff --git a/setup.sh b/setup.sh index a5cb8a4..221ebb9 100755 --- a/setup.sh +++ b/setup.sh @@ -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 \ No newline at end of file