Method Contains of IRelationalOperator doesn't work well

785
7
01-11-2012 05:29 AM
SergioMunoz
New Contributor
Hello everyone,

I belong to a company which has bought a license for ArcGIS. We are still learning how to use it, and I have a problem with Contains method. I have a simple polygon and a point, created like this:

            //Create the centroid of the arc
            IPoint tempPoint = new PointClass();
            tempPoint.PutCoords(x, y);

            //Create an arc with given radius
            ICircularArc pConstructCArc = new CircularArcClass();
            pConstructCArc.PutCoordsByAngle(tempPoint, fromAngle, centralAngle, radius);
            ILine line = new LineClass();
            line.PutCoords(tempPoint, pConstructCArc.ToPoint);
            
            //Add the arc segment to a polygon geometry
            ISegmentCollection pSegmentCollection = new PolygonClass();
            pSegmentCollection.AddSegment((ISegment)line, ref missing, ref missing);
            pSegmentCollection.AddSegment((ISegment)pConstructCArc, ref missing, ref missing);
            PolygonClass pPolygon = (PolygonClass)pSegmentCollection;
            pPolygon.Close();
            pPolygon.SpatialReference = map.SpatialReference;

            IPoint target = new PointClass();
            target.PutCoords(i, j);
            
            if (pPolygon.Contains(target))
            {
                 //Do whatever
            }


I am pretty sure that the point is contained into the polygon, because I have painted both of them, and the result is this:

[ATTACH=CONFIG]11076[/ATTACH]

However, Contains method is always returning false, even I have created a simple triangle like this:

[ATTACH=CONFIG]11079[/ATTACH]

And it still returns false. Am I doing anything wrong?

Thank you very much!
0 Kudos
7 Replies
sapnas
by
Occasional Contributor III
can you also post the code that creates irelationaloperator object arc?
0 Kudos
by Anonymous User
Not applicable
Original User: smunoz

Actually, there is not an IRelationalOperator object arc. The object that implements the IRelationalOperator interface is the polygon pPolygon, which contains the arc and the line, so this is all the code I use.

Besides, when I change the code to use a simple triangle instead an arc, it stills returns false:

            IPoint tempPoint = new PointClass();
            tempPoint.PutCoords(x, y);

            //Create an arc with given radius
            ICircularArc pConstructCArc = new CircularArcClass();
            pConstructCArc.PutCoordsByAngle(tempPoint, fromAngle, centralAngle, radius);
            ILine line = new LineClass();
            line.PutCoords(tempPoint, pConstructCArc.ToPoint);
            
            ILine line2 = new LineClass();
            line2.PutCoords(pConstructCArc.ToPoint, pConstructCArc.FromPoint);

            //Add the arc segment to a polygon geometry
            ISegmentCollection pSegmentCollection = new PolygonClass();
            pSegmentCollection.AddSegment((ISegment)line, ref missing, ref missing);
            pSegmentCollection.AddSegment((ISegment)line2, ref missing, ref missing);
            PolygonClass pPolygon = (PolygonClass)pSegmentCollection;
            pPolygon.Close();
            pPolygon.SpatialReference = map.SpatialReference;
0 Kudos
sapnas
by
Occasional Contributor III
I was referring to the arc.Contains(target)) statement. Are you assigning pPolygon to "arc".
0 Kudos
by Anonymous User
Not applicable
Original User: smunoz

Sorry, my mistake. How bad is the copy/paste thechnique...

I have edited the first message.
0 Kudos
sapnas
by
Occasional Contributor III
You may have to provide the data. Add the below logic and post the resulting string sXML.

IXMLSerializer xmlSer = new XMLSerializerClass();
string sXML = xmlSer.SaveToString(pPolygon,null,null);

Can you also provide i and j values?

I also noticed that you are not assigning spatial reference to the target. Can you set the spatial reference prior to contains statement?
0 Kudos
by Anonymous User
Not applicable
Original User: smunoz

Ok, I have set the spatial reference for the target this way:

target.SpatialReference = pPolygon.SpatialReference


but it stills returns false. I have obtained the serialization xml and it is the following:

<PolygonN xsi:type='typens:PolygonN'  xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'  xmlns:xs='http://www.w3.org/2001/XMLSchema' xmlns:typens='http://www.esri.com/schemas/ArcGIS/9.3'>
 
 <HasID>false</HasID>
 <HasZ>false</HasZ>
 <HasM>false</HasM>
 <Extent xsi:type='typens:EnvelopeN'>
  <XMin>-2.3246327256281116</XMin>
  <YMin>36.841729542709459</YMin>
  <XMax>-2.2962178854146593</XMax>
  <YMax>36.858816661584896</YMax>
 </Extent>
 <RingArray xsi:type='typens:ArrayOfRing'>
  <Ring xsi:type='typens:Ring'>
   <SegmentArray xsi:type='typens:ArrayOfSegment'>
    <Segment xsi:type='typens:Line'>
     <FromPoint xsi:type='typens:PointN'>
      <X>-2.3104253055213855</X>
      <Y>36.841729542709459</Y>
     </FromPoint>
     <ToPoint xsi:type='typens:PointN'>
      <X>-2.3246327256281116</X>
      <Y>36.851222637324717</Y>
     </ToPoint>
    </Segment>
    <Segment xsi:type='typens:CircularArc'>
     <FromPoint xsi:type='typens:PointN'>
      <X>-2.2962178854146593</X>
      <Y>36.851222637324717</Y>
     </FromPoint>
     <ToPoint xsi:type='typens:PointN'>
      <X>-2.3246327256281116</X>
      <Y>36.851222637324717</Y>
     </ToPoint>
     <CenterPoint xsi:type='typens:PointN'>
      <X>-2.3104253055213855</X>
      <Y>36.841729542709459</Y>
     </CenterPoint>
     <IsCounterClockwise>true</IsCounterClockwise>
     <IsMinor>true</IsMinor>
     <IsLine>false</IsLine>
    </Segment>
    <Segment xsi:type='typens:Line'>
     <FromPoint xsi:type='typens:PointN'>
      <X>-2.3246327256281116</X>
      <Y>36.851222637324717</Y>
     </FromPoint>
     <ToPoint xsi:type='typens:PointN'>
     <X>-2.3104253055213855</X>
     <Y>36.841729542709459</Y>
     </ToPoint>
    </Segment>
   </SegmentArray>
  </Ring>
 </RingArray>
 <SpatialReference xsi:type='typens:GeographicCoordinateSystem'>
  <WKT>GEOGCS[&quot;GCS_WGS_1984&quot;,DATUM[&quot;D_WGS_1984&quot;,SPHEROID[&quot;WGS_1984&quot;,6378137.0,298.257223563]],PRIMEM[&quot;Greenwich&quot;,0.0],UNIT[&quot;Degree&quot;,0.0174532925199433],AUTHORITY[&quot;EPSG&quot;,4326]]</WKT>
  <XOrigin>-400</XOrigin>
  <YOrigin>-400</YOrigin>
  <XYScale>11258999068426.24</XYScale>
  <ZOrigin>-100000</ZOrigin>
  <ZScale>10000</ZScale>
  <MOrigin>-100000</MOrigin>
  <MScale>10000</MScale>
  <XYTolerance>8.983152841195215e-009</XYTolerance>
  <ZTolerance>0.00020000000000000001</ZTolerance>
  <MTolerance>0.00020000000000000001</MTolerance>
  <HighPrecision>true</HighPrecision>
  <LeftLongitude>-180</LeftLongitude>
  <WKID>4326</WKID>
 </SpatialReference>
</PolygonN>


And the values of the point are the following:
i --> -2.31288
j --> 36.85033
0 Kudos
sapnas
by
Occasional Contributor III
You may have to use project method to set the spatial reference for pPolygon and target.
0 Kudos