Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Province from coordinate #361

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

Moeinmn
Copy link

@Moeinmn Moeinmn commented Feb 19, 2024

Added findProvinceFromCoordinate module - tests passed and readme update

@ghost
Copy link

ghost commented Feb 19, 2024

👇 Click on the image for a new way to code review

Review these changes using an interactive CodeSee Map

Legend

CodeSee Map legend

@mediv0 mediv0 self-requested a review February 20, 2024 17:53
@mediv0
Copy link
Member

mediv0 commented Feb 21, 2024

irGeoJSON.ts file is almost 5Mb in size
The bundle size gonna be huge

@Moeinmn
Copy link
Author

Moeinmn commented Feb 25, 2024

Dear @mediv0
I compressed and removed redundant data from Geojson and reduced the size to 360KB.

&& (point.longitude < ((polygon[j][0] - polygon[i][0]) * (point.latitude - polygon[i][1]) / (polygon[j][1] - polygon[i][1]) + polygon[i][0]))) {
odd = !odd;
}
j = i;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please move the j = i inside the for loop

for (let i = 0, j = polygon.length - 1; i < polygon.length; j = i++) { ... }

const provinces = GeoJSONData;

function pointInPolygon(polygon: number[][], point: Point): boolean {
let odd = false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the variable odd doesn't make any sense here ( naming )
please change it to something more meaningful
like intersect isIntersecting

Comment on lines 29 to 30
if (((polygon[i][1] > point.latitude) !== (polygon[j][1] > point.latitude))
&& (point.longitude < ((polygon[j][0] - polygon[i][0]) * (point.latitude - polygon[i][1]) / (polygon[j][1] - polygon[i][1]) + polygon[i][0]))) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using the Ray casting algorithm is good idea here but this if statement is so cluttered and hard to read

you could define some local variables to hold values instead of using them directly like polygon[i][1]

const xi = polygon[i][0];
const yi = polygon[i][1];
const xj = polygon[j][0]; 
const yj = polygon[j][1];


const {  longitude: x, latitude: y } = point

// so much simpler to read 
((yi > y) !== (yj > y)) && (x < (xj - xi) * (y - yi) / (yj - yi) + xi);

let foundProvince: GeoJSONFeature | undefined;
for (let index = 0; index < provinces.features.length; index++) {
const province = provinces.features[index];
const isInsideProvince = pointInPolygon(province.geometry.coordinates[0][0], pointToCheck);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make another variable here

const coordinates = province.geometry.coordinates[0][0];

Comment on lines 61 to 66
const normalizedProvinceObject: Province = {
properties: {
'name:fa': foundProvince.properties.name || "",
'name:en': foundProvince.properties['name:en'] || "",
},
geometry: foundProvince.geometry
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are you having keys like name:fa ?
it would not be clean to destruct this object and get the result

i would suggest change to something like

const normalizedProvinceObject: Province = {
			name: {
				fa: foundProvince.properties.name || "",
			  en: foundProvince.properties['name:en'] || "",
			},
			geometry: foundProvince.geometry
}

// accessing
// .1
const {fa, en} = province.name

// .2
const { name } = province
name.fa // تهران
name.en // tehran

Comment on lines 16 to 22
interface Province {
properties: {
'name:fa': string;
'name:en': string;
};
geometry: object;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. what is the point of showing users geometry of the given coordinates
  • as the functions indicates, you will provide a set of coordinate and get a result respectively

it would be better to remove this property entirely and focus on the main purpose of this function which is finding a province

something like:

const { fa, en } = findProvinceFromCoordinate(...)
  1. it should have a type anything other than raw object

@Moeinmn
Copy link
Author

Moeinmn commented Feb 27, 2024

Thank you for your comments @mediv0 ,
I've updated the code based on your comments.

@mediv0
Copy link
Member

mediv0 commented Feb 27, 2024

Thank you for your comments @mediv0 ,
I've updated the code based on your comments.

That's awesome I'll check tomorrow
Nice job soltan 😄

@Moeinmn Moeinmn closed this Feb 28, 2024
@Moeinmn
Copy link
Author

Moeinmn commented Feb 28, 2024

Thank you for your comments @mediv0 ,
I've updated the code based on your comments.

That's awesome I'll check tomorrow Nice job soltan 😄

Lot's of Eradaat 😉✌️

@Moeinmn Moeinmn reopened this Feb 28, 2024
@mediv0
Copy link
Member

mediv0 commented Feb 29, 2024

@ali-master what do you think ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants