Skip to content

Commit

Permalink
done
Browse files Browse the repository at this point in the history
Update update-mmdb.yaml

Added workflow to build image. (ChakshuGautam#42)

tracks and adds west bengal

feat: modify script to download available village boundary data

feat: update Dockerfile to fix deployment (ChakshuGautam#55)
  • Loading branch information
kumarguddu609 committed Feb 12, 2024
1 parent 35bd5b9 commit 87302f6
Show file tree
Hide file tree
Showing 55 changed files with 195 additions and 35 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/server/geojson-data/indian_village_boundaries.zip/west_bengal/** filter=lfs diff=lfs merge=lfs -text
46 changes: 46 additions & 0 deletions .github/workflows/build-and-push.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: Build and Push Docker Image

on:
push:
branches:
- master

env:
DOCKER_USERNAME: ${{ github.actor }}
DOCKER_IMAGE_NAME: ${{ github.repository }}
DOCKER_REGISTRY: ghcr.io
DOCKER_IMAGE_TAG: ${{ github.ref_name }}

jobs:
build-and-push:
runs-on: ubuntu-latest
steps:
- id: lower-repo
shell: pwsh
run: |
"::set-output name=repository::$($env:DOCKER_IMAGE_NAME.ToLowerInvariant())"
- name: Checkout code
uses: actions/checkout@v2

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Login to Docker registry
uses: docker/login-action@v1
with:
registry: ${{ env.DOCKER_REGISTRY }}
username: ${{ env.DOCKER_USERNAME }}
password: ${{ secrets.PAT }}

- name: Build and Push Docker image
uses: docker/build-push-action@v4
with:
build-args: |
"SERVER_RELEASE_VERSION=${{ github.sha }}"
context: "server/."
push: true
cache-from: type=gha
cache-to: type=gha,mode=max
tags: ${{ env.DOCKER_REGISTRY }}/${{ steps.lower-repo.outputs.repository }}:${{env.DOCKER_IMAGE_TAG}}
labels: org.opencontainers.image.source=https://github.com/${{steps.lower-repo.outputs.repository}}
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:


- name: Install dependencies
run: bun install
run: ./setup.sh
working-directory: ./geoip/server # Set the working directory to your project root

- name: Run bun test
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/update-mmdb.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Download and Commit File

on:
schedule:
- cron: '0 0 * * *' # This will run the action daily at midnight
- cron: '0 12 * * 0' # This will run the action daily at midnight
workflow_dispatch: # This allows manual triggering

jobs:
Expand All @@ -26,4 +26,4 @@ jobs:
- name: Commit and Push
uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: Update db.mmdb
commit_message: Update db.mmdb
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
Binary file modified bun.lockb
Binary file not shown.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
"peerDependencies": {
"typescript": "^5.0.0"
}
}
}
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ See code example in the [server](./server/app.js) folder.

Run `./setup.sh`. This script will install bun and download required files to setup server
```sh
cd server
./setup.sh
```

Start Server
```sh
cd server
bun app.js
```

Expand Down Expand Up @@ -42,4 +42,4 @@ DB will remain updated automatically. Please create a ticket if you see some iss

#### Contribution Guide
1. Please consider issues up from grabs.
2. It will only be assigned with a PR.
2. It will only be assigned with a PR.
6 changes: 5 additions & 1 deletion server/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ WORKDIR /usr/src/app

COPY . .

RUN apt-get update && apt-get install -y \
curl
CMD /bin/bash
COPY ./package*.json ./bun.lockb ./
RUN bun install
RUN ./setup.sh
RUN ./setup.sh

ENV NODE_ENV production

Expand Down
4 changes: 2 additions & 2 deletions server/__tests__/sample.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ describe('GET /georev', () => {

describe('GET /location/:locationlevel/centroid', () => {
it ('should return lat lon for given district name in query', async() => {
const response = await fetch(`${BASE_URL}/location/DISTRICT/centroid?zone=lucknow`);
const response = await fetch(`${BASE_URL}/location/DISTRICT/centroid?query=lucknow`);
const jsonResponse = await response.json();
expect(response.status).toBe(200);
expect(jsonResponse).toEqual({
Expand All @@ -175,7 +175,7 @@ describe('GET /location/:locationlevel/centroid', () => {
})

it ('should return error for invalid district name in query', async() => {
const response = await fetch(`${BASE_URL}/location/DISTRICT/centroid?zone=lalaland`);
const response = await fetch(`${BASE_URL}/location/DISTRICT/centroid?query=lalaland`);
expect(response.status).toBe(404);
})
});
3 changes: 2 additions & 1 deletion 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 Expand Up @@ -234,4 +235,4 @@ app.hostname = '0.0.0.0';


swaggerApp.listen(3001, () => console.log('Swagger listening on port 3000'))
app.listen();
app.listen();
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
3 changes: 2 additions & 1 deletion server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"maxmind-db-reader": "^0.2.1",
"mmdb-lib": "^2.0.2",
"swagger-jsdoc": "^6.2.8",
"swagger-ui-express": "^5.0.0"
"swagger-ui-express": "^5.0.0",
"fuse.js": "^7.0.0"
},
"jest": {
"testEnvironment": "node",
Expand Down
1 change: 0 additions & 1 deletion server/pickled_output.json

This file was deleted.

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));
31 changes: 25 additions & 6 deletions setup.sh → server/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,38 @@ fi
bun install

# Changing PWD to download geojson data using curl
mkdir server/geojson-data &> /dev/null
cd server/geojson-data
mkdir ./geojson-data &> /dev/null
cd geojson-data

curl -Lo INDIA_DISTRICT.geojson "https://github.com/datta07/INDIAN-SHAPEFILES/raw/master/INDIA/INDIA_DISTRICTS.geojson"
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
# 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 /server/
cd ../..

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

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

0 comments on commit 87302f6

Please sign in to comment.