Hello.
Here is the C# code snippet I used.
`Shapefile ch_sf = axMap1.get_GetObject(Convert.ToInt32(layer1CBox.SelectedValue)) as Shapefile;
Shapefile rl_sf = axMap1.get_GetObject(Convert.ToInt32(layer2CBox.SelectedValue)) as Shapefile;
if (ch_sf != null && rl_sf != null)
{
object ShapeIDs = null;
if (ch_sf.GetRelatedShapes(Convert.ToInt32(layer2CBox.SelectedValue), (MapWinGIS.tkSpatialRelation)spatialRelCBox.SelectedIndex, ShapeIDs))
{
int[] shapes = ShapeIDs as int[];
if (shapes != null)
{
for (int i = 0; i < shapes.Length; i++)
{
ch_sf.ShapeSelected[shapes[i]] = true;
}
label4.Text = string.Format("{0} objects found", shapes.Length);
}
axMap1.Redraw();
}
label4.Text = string.Format("0 objects found");
}`
However, I get false result from GetRelatedShapes function for all my tested shapefiles. In this case shapes arent identified. And there is no any other exceptions coming out there. In MapWindow 5 spatial query works.
Rather than calling GetRelatedShapes, you should perhaps be calling GetRelatedShapes2, or possibly SelectByShapefile.
GetRelatedShapes compares a single shape (referenced by it’s index) with the remaining shapes all within the same Shapefile.
GetRelatedShapes2, on the other hand, compares a single external shape with the entire contents of the current shapefile.
Finally, SelectByShapefile takes references to two distinct shapefiles, comparing all of the contents. It looks like this is what you might be trying to do.
It’s hard to tell from the code snippet what actual values are being passed (or what you are intending to pass) to each of the functions, since they are all coming out of ComboBox values.
I hope this helps. Please let me know if more clarification is necessary.
A big Thank you for your suggestions, @jerryfaust! From comboboxes I am passing layer handle values.
I have tried “SelectByShapefile” function. Changed the line from if (ch_sf.GetRelatedShapes(Convert.ToInt32(layer2CBox.SelectedValue), (MapWinGIS.tkSpatialRelation)spatialRelCBox.SelectedIndex, ShapeIDs))
to if (ch_sf.SelectByShapefile(rl_sf, (MapWinGIS.tkSpatialRelation)spatialRelCBox.SelectedIndex, false, ShapeIDs, null))
Sorry to say, it acts the same way…
One thing I’ve noticed that first shape object treated in debugger as COM-object while the second one is Shapefile class. Could it make any sense?
Are they both actually shapefiles? as opposed to OGR layers, for example?
One thing to try is other spatial relation tests; my recollection is that SelectByShapefile reverses the order of the test (compared with GetRelatedShapes), such that if one function call was using Contains, the other function call would use Within. Does that make sense?
I would just try different options, specifically Intersection, since it is most inclusive.
Regards.
Addendum: Regarding the Object vs Shapefile, try using get_Shapefile rather than get_Object.
Changed the function to get_Shapefile. Now the shapefiles appear as COM-objects.
Now I tried to use shapes created my own. In debugger sometimes I notice that in ShapeIDs array there is a value like int[0] but it feels like there is nothing contained. And sometimes ShapeIDs is null despite the fact I dont change spatial relation type in both cases. Weird. Functions still return false.
I have tried it, it doesnt make sense. Any option I choose, it selects right type, but functions “GetRelatedShapes”, “SelectByShapefile” dont change their behaviour.
I’m sorry for the troubles. I agree that it doesn’t make sense. If you don’t mind attaching those two files in your last example, I can step through the OCX itself and see what’s going on.
I downloaded the shapefiles and wrote this quick test, and it shows all 12 points within the polygon. So I’m not sure what is happening, but perhaps something is different in your environment.
Perhaps you could strip it down to the bare minimum to verify if it works, then build back from there?
Or if you don’t mind posting your entire app, I could look a little deeper? I’d like to help if I can.
Thank you for your effort, @jerryfaust! I solved my problem. In this case I needed to put “ref” keyword before the array variable. Function required a reference variable for an output array. So that’s why the function couldnt send an array outside of the function.
I don’t use c# as much as I do VB.NET, so I didn’t catch that. To me, that’s one of the mysteries of c# (although I imagine there’s a good reason), the compiler knows it’s a reference parameter, why do we have to tell it?
However, for future reference, you should know that our online helpfile is built based on c# templates, so it will document those cases in which ‘ref’ is required. For example, consider the link for SelectByShapefile. It does indicate that Result is a reference object.