Check if GPS is within a polygonal area

can you help me in my code.
i want to check if the gps location is within the polygon or not... and the location that i take from the gps module is within the polygon but the result of my code does not work correctly . the result say that the location is not in polygon ..
I'm sure that i have a mistake in the code but i can't see it

Thank You :slight_smile:
My Code:
#include <SoftwareSerial.h>

SoftwareSerial gpsSerial(10, 11); // RX, TX (TX not used)
int leda = 7;
int ledt = 12;
const int sentenceSize = 80;
String Data1="";
String Data2="";
String Data3="";
String Data4="";
String Data5="";

int crossings = 0;
char str0[20];
char str1[20];
char str2[20];
const int npoint=6;
float x[npoint]={351448.94, 351926.15, 351915.85, 351479.83, 350858.15, 350734.56};
float y[npoint]={323750.32, 323670.58, 323303.68, 323212.29, 323245.66,323663.33 };
char sentence[sentenceSize];
void setup()
{
Serial.begin(115200);
gpsSerial.begin(115200);
}

void loop()
{
static int i = 0;
if (gpsSerial.available())
{
char ch = gpsSerial.read();
if (ch != '\n' && i < sentenceSize)
{
sentence = ch;

  • i++;*
  • }*
  • else*
  • {*
    _ sentence = '\0';_
    * i = 0;*
    * displayGPS();*
    * }*
    * }*

}
void displayGPS()
{
_ char field[20],**end1;;_

* getField(field, 0);*
* if (strcmp(field, "$GPRMC") == 0)*
* {*
* Serial.print("Lat: ");*
* getField(field, 3); // number*
* double f1,f,ff,ffw;*
* // f= atof(field);*
* //double num;*
* f = strtod( field, end1 ); // to double*
* ff=f/100.0000;*
* //Serial.println(f);*
* f1=ff-32;*
_ ffw=f1100;_
_
dtostrf(ffw,6,6,str0);_
_
Data1=str0;_
_
float minit,minitm;_
_
minit=(atoi(str0));_
_
//Serial.println(minit);_
_
minitm=minit/60;_
_
//Serial.println(minitm);_
_
double sec;_
_
sec=f-3200-minit;_
_ sec=sec60;
* //Serial.println(sec);*
* sec=sec/36;*
* //Serial.println(sec);*
* double lat;*
lat=3200+(minitm100)+sec;_
_
lat=lat/100.0;_
_
dtostrf(lat,6,6,str0);_
_
Data1=str0;_
_
Serial.print(Data1);_
_
/////*_

* //////*
_ lat=lat10000;_
_
dtostrf(lat,6,6,str0);_
_
// Serial.print(str0);*_

* getField(field, 4); // N/S*
* Serial.print(field);*
* Data2=field;*

* Serial.print(" Long: ");*
* getField(field, 5); // number*
* f = strtod( field, end1 ); // to double*
* ff=f/100.0000;*
* //Serial.println(f);*
* f1=ff-35;*
_ ffw=f1100;_
_
dtostrf(ffw,6,6,str0);_
_
Data1=str0;_
_
// float minit,minitm;_
_
minit=(atoi(str0));_
_
//Serial.println(minit);_
_
minitm=minit/60;_
_
//Serial.println(minitm);_
_
//double sec;_
_
sec=f-3500-minit;_
_ sec=sec60;
* //Serial.println(sec);*
* sec=sec/36;*
* //Serial.println(sec);*
double Long;
Long=3500+(minitm100)+sec;_
_
Long=Long/100.0;_
_
dtostrf(Long,6,6,str1);_
_
//Serial.print(str1);_
_
Data1=str1;_
_
Serial.print(Data1);_
_ Long=Long10000;_

* dtostrf(Long,6,6,str1);*
* // Serial.print(str1);*
* getField(field, 6); // E/W*
Serial.print(field);
* Data4=field;*
if(inpoly(atof(str1),atof(str0))==1) {
* Serial.println("in polygon ");*
* delay (5000);*
* }*
* else if (inpoly(atof(str1),atof(str0))==2)*
* {*
* Serial.println("out of polygon ");*
* delay (5000);*
* }*

* }*
}
int inpoly(float px, float py)
{
* int i, j, c = 0;*
* for (i = 0, j = npoint-1; i < npoint; j = i++) {*
_ if ( ((y*>py) != (y[j]>py)) &&
(px < (x[j]-x) * (py-y) / (y[j]-y) + x) ){
c = 1;*_

* }*
* else {*
* c=2;*

* }}*
return c;
}
void getField(char* buffer, int index)
{
* int sentencePos = 0;*
* int fieldPos = 0;*
* int commaCount = 0;*
* while (sentencePos < sentenceSize)*
* {*
* if (sentence[sentencePos] == ',')*
* {*
* commaCount ++;*
* sentencePos ++;*
* }*
* if (commaCount == index)*
* {*
* buffer[fieldPos] = sentence[sentencePos];*
* fieldPos ++;*
* }*
* sentencePos ++;*
* }*
* buffer[fieldPos] = '\0';*
}

can you help me in my code.

Not until you post it correctly. There are stickies at the top of the forum. Read both of them, then fix your post.

The general way to determine whether a given point is inside or outside a polygon is to write a line intersection algorithm and then implement a line segment intersection algorithm on top of that. Then add the number of intersections between each segment of the polygon, and the line from your target point to a reference point. If the total number of intersections is even, the two points are on the same side of the boundary i.e. both inside or both outside.

Simpler solutions may be possible if you can impose restrictions on the nature of the polygon.

This algorithm for checking polygon boundaries has been around for some time and although it appears similar to yours, there are also some significant differences. c - Point in Polygon Algorithm - Stack Overflow

You need to be careful with the units that you are using. Most GPS devices encode the coordinates in a format which looks like a number, but for which normal geometric algebra won't work.

      sentence = ch;

I don't believe that code even compiles.

I think you need to get together with your classmates and work this problem out yourselves, instead of each and every one of you posting a new thread about the same problem.

Get rid of those stupid Strings. Not a single one of them is doing anything but pissing away resources uselessly.