0

I am trying to create a smart contract that will store an array containing GPS coordinates such as:

X=[(31.332,54.213),(32.013,54.312), (33.124,56.2134),(34.123, 57.789), (34.789,59.654),(31.332,54.213)] 

The goal of this is to create a polygon and compare incoming gps coordinates whether inside or outside this polygon(point in polygon problem). I haven't been able to find any decent solution on how to create this array of a list of lat and long coordinates. Everyone suggests using strucs but they seem to be creating a new data type. I was thinking of nesting the strucs for each vertex into one array, but I can't find a way of doing it. Thanks in advance.

0

I created this just for you:

contract omega {
mapping (uint => uint[3]) public GPS;

function omega(uint[] coords) public{
    uint x = uint(coords.length)/uint(3);
    uint cont = 0;
    for(uint r = 0; r<x;r++){
        setXYZ(r,coords[cont],coords[cont+1],coords[cont+2]);
        cont+=2;
    }
}

function setXYZ(uint index, uint _x,uint _y,uint _z) public{
    setX(index,_x);
    setY(index,_y);
    setZ(index,_z);
}

function setX(uint index,uint _value){
    GPS[index][0] = _value;
}
function setY(uint index,uint _value){
    GPS[index][1] = _value;
}
function setZ(uint index,uint _value){
    GPS[index][2] = _value;
}
}

I hope it helps you :). Have a good day

  • Thank you very much i appreciate it, but would you be able to explain the code am not sure how it behaves? – Wtaba Mar 4 '18 at 13:06
  • I have been playing around with the code. It seems that whenever i am putting in the coordinates it is only giving the output of the first index? I am not sure if am placing the correct values in. – Wtaba Mar 4 '18 at 15:20
  • Yeah, its simple, if you know about mapping things, you should add the array of your coordinates as [1,2,3,4,5,6] instead of [[1,2,3],[4,5,6]], this contract will add your entire array as a map with the index of every three values. – ReptilianGK Mar 5 '18 at 16:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.