intersection

1495
8
01-04-2017 08:42 AM
BrettDavidson
New Contributor II

Find E

The Question: Can ArcGIS SDK for iOS in Objective C find this solution with precision utilizing Lat/Long coordinates for ABCD to find E? if so! HOW?

0 Kudos
8 Replies
BrettDavidson
New Contributor II

How do you input for the coordinates for lineAB? This would be two sets of coordinates. I have not dealt with ArcGIS SDK for iOS yet. Can you explain for me?

0 Kudos
RyanOlson1
Esri Contributor

Actually - you should get back a line when intersecting 2 lines. My first answer above is incorrect, I will remove it for clarity.

Code should look something like this:

-(void)lineIntersection{
    AGSPolyline *lineAB = [AGSPolyline polylineWithPoints:@[AGSPointMakeWGS84(0, 0), AGSPointMakeWGS84(10, 10)]];
    AGSPolyline *lineCD = [AGSPolyline polylineWithPoints:@[AGSPointMakeWGS84(0, 10), AGSPointMakeWGS84(10, 0), AGSPointMakeWGS84(5, 10), AGSPointMakeWGS84(0, 0), AGSPointMakeWGS84(10, 10)]];
    AGSPolyline *lineE = (AGSPolyline*)[AGSGeometryEngine intersectionOfGeometry1:lineAB geometry2:lineCD];
    NSLog(@"lineE: %@", lineE);
}
BrettDavidson
New Contributor II

Hello,

I got NSLog reply of:
lineE: AGSPolyline: [], sr: 4326

How do I get the lat/long from lineE?

0 Kudos
MichaelDavis3
Occasional Contributor III

Wouldn't the AGSGeometry returned by the Geometry Engine be a point, not a polyline?

0 Kudos
RyanOlson1
Esri Contributor

Brett,

Nick Furness pointed out to me that you need to actually union the lines together first to get what you are looking for.

Here is objective-c code to do what you need:

@interface AGSPolyline (intersects)

-(NSArray<AGSPoint*>*)intersectionWithLine:(AGSPolyline*)line2;

@end


@implementation AGSPolyline (intersects)

-(NSArray<AGSPoint*>*)intersectionWithLine:(AGSPolyline*)line2{
    
    
    NSMutableArray *points = [NSMutableArray array];
    
    // first union the lines together
    AGSPolyline *lineUnion = (AGSPolyline*)[AGSGeometryEngine unionOfGeometry1:self geometry2:line2];
    
    // now get the intersecting line of union'ed line and self
    AGSPolyline *line3 = (AGSPolyline*)[AGSGeometryEngine intersectionOfGeometry1:self geometry2:lineUnion];
    
    double tolerance = 1e-10;
    
    for (AGSPart *part in line3.parts){
        for (AGSPoint *point in part.points){
            double distance = [AGSGeometryEngine distanceBetweenGeometry1:point geometry2:line2];
            
            // if the point is on the passed-in line then add it to the return value
            if (distance < tolerance){
                [points addObject:point];
            }
        }
    }
    
    return [points copy];
}

@end


-(void)testLineIntersection{
    AGSPolyline *lineAB = [AGSPolyline polylineWithPoints:@[AGSPointMakeWGS84(0, 0), AGSPointMakeWGS84(10, 10)]];
    AGSPolyline *lineCD = [AGSPolyline polylineWithPoints:@[AGSPointMakeWGS84(0, 10), AGSPointMakeWGS84(10, 0)]];
    
    NSArray *pointsE = [lineAB intersectionWithLine:lineCD];
    
    NSLog(@"intersection points: %@", pointsE);
}

Thanks to Nick Furness for pointing this out. He also provided the swift code, which I converted to objective-c.

Nicholas-Furness
Esri Regular Contributor

Thanks for posting that, Ryan Olson‌.

For reference, here's a Gist with the Swift code: Runtime Line Intersection Points · GitHub 

The Gist also discusses a potential edge-case worth being aware of, depending on how your input data is generated.

BrettDavidson
New Contributor II

Input data is generated, by manual input on:

CGPoint   p1 = CGPointMake(32.734444, -116.577499);CGPoint   p2 = CGPointMake(32.7337, -117.008);CGPoint   p3 = CGPointMake(33.363609, -116.836388);CGPoint   p4 = CGPointMake(32.64, -116.836);

-(void)lineIntersection{

AGSPolyline *lineAB = [AGSPolyline polylineWithPoints:@[AGSPointMakeWGS84(p1), AGSPointMakeWGS84(p2)]];

AGSPolyline *lineCD = [AGSPolyline polylineWithPoints:@[AGSPointMakeWGS84(p3), AGSPointMakeWGS84(p4)]];


NSArray *pointsE = [lineAB intersectionWithLine:lineCD];

NSLog(@"intersection points: %@", pointsE);


}

0 Kudos
BrettDavidson
New Contributor II

Thank you sir I will try as soon as I get time. I currently have 42 warnings from the install of the SDK, trying to work them out..

0 Kudos