# GetRelatedShapes function always return false

**URL:** <https://mapwindow.discourse.group/t/getrelatedshapes-function-always-return-false/518>\
**Category:** MapWinGIS\
**Created:** [March 29, 2020, 12:14pm UTC](https://mapwindow.discourse.group/t/getrelatedshapes-function-always-return-false/518 "2020-03-29T12:14:44Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Windground](https://avatars.discourse-cdn.com/v4/letter/w/9fc348/32.png) [@Windground](https://mapwindow.discourse.group/u/Windground)\
**Post date:** [March 29, 2020, 12:14pm UTC](https://mapwindow.discourse.group/t/getrelatedshapes-function-always-return-false/518/1 "2020-03-29T12:14:44Z")

</div>

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.

 ![mapwindow_result](https://global.discourse-cdn.com/free1/uploads/mapwindow1/original/1X/1fde645750ce92c75e007e2efee5083ef605da7f.png)

---

<div class="post-metadata">

**Author:** ![jerryfaust](https://yyz2.discourse-cdn.com/free1/user_avatar/mapwindow.discourse.group/jerryfaust/32/378_2.png) [@jerryfaust](https://mapwindow.discourse.group/u/jerryfaust)\
**Post date:** [March 29, 2020, 5:47pm UTC](https://mapwindow.discourse.group/t/getrelatedshapes-function-always-return-false/518/2 "2020-03-29T17:47:33Z")

</div>

Hello @Windground, and welcome.

Rather than calling `GetRelatedShapes`, you should perhaps be calling `GetRelatedShapes2`, or possibly `SelectByShapefile`.

[GetRelatedShapes](https://www.mapwindow.org/documentation/mapwingis4.9/group __shapefile__ selection.html#ga5842d300cb2fcb34759df3e32c1f5917) compares a single shape (referenced by it’s index) with the remaining shapes **all within the same Shapefile**.

[GetRelatedShapes2](https://www.mapwindow.org/documentation/mapwingis4.9/group __shapefile__ selection.html#gabd57ab3b4563ccdda0f4456cb8ee4201), on the other hand, compares **a single external shape** with the entire contents of the current shapefile.

Finally, [SelectByShapefile](https://www.mapwindow.org/documentation/mapwingis4.9/group __shapefile__ geoprocessing.html#ga1a0f9f0c07d9a5f4965e69070fdb4568) 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.

Regards,  
Jerry.

---

<div class="post-metadata">

**Author:** ![Windground](https://avatars.discourse-cdn.com/v4/letter/w/9fc348/32.png) [@Windground](https://mapwindow.discourse.group/u/Windground)\
**Post date:** [March 29, 2020, 6:45pm UTC](https://mapwindow.discourse.group/t/getrelatedshapes-function-always-return-false/518/3 "2020-03-29T18:45:22Z")

</div>

> [@jerryfaust](#):
>
> SelectByShapefile

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?

 ![image](https://global.discourse-cdn.com/free1/uploads/mapwindow1/original/1X/d362c911681d15684415d3fe37433052421d014d.png)

---

<div class="post-metadata">

**Author:** ![jerryfaust](https://yyz2.discourse-cdn.com/free1/user_avatar/mapwindow.discourse.group/jerryfaust/32/378_2.png) [@jerryfaust](https://mapwindow.discourse.group/u/jerryfaust)\
**Post date:** [March 29, 2020, 7:38pm UTC](https://mapwindow.discourse.group/t/getrelatedshapes-function-always-return-false/518/4 "2020-03-29T19:38:36Z")

</div>

Hello again.

1. Are they both actually shapefiles? as opposed to OGR layers, for example?
2. 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](https://www.mapwindow.org/documentation/mapwingis4.9/group __map__ layer__management.html#gad42f479b7aca0c8424639a9b619b6cc2) rather than get\_Object.

---

<div class="post-metadata">

**Author:** ![Windground](https://avatars.discourse-cdn.com/v4/letter/w/9fc348/32.png) [@Windground](https://mapwindow.discourse.group/u/Windground)\
**Post date:** [March 30, 2020, 6:33am UTC](https://mapwindow.discourse.group/t/getrelatedshapes-function-always-return-false/518/5 "2020-03-30T06:33:32Z")

</div>

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.

 ![1](https://global.discourse-cdn.com/free1/uploads/mapwindow1/original/1X/ad10f9c4a02f174f58b73fbb6e5c80e96d2f1bc6.png)

> [@jerryfaust](#):
>
> Are they both actually shapefiles? as opposed to OGR layers, for example?

Yeah, they are actual .shp files.

> [@jerryfaust](#):
>
> 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 have tried it, it doesnt make sense. Any option I choose, it selects right type, but functions “GetRelatedShapes”, “SelectByShapefile” dont change their behaviour.

> [@jerryfaust](#):
>
> I would just try different options, specifically **Intersection** , since it is most inclusive.

I tried it, but it still doesn’t work.

---

<div class="post-metadata">

**Author:** ![jerryfaust](https://yyz2.discourse-cdn.com/free1/user_avatar/mapwindow.discourse.group/jerryfaust/32/378_2.png) [@jerryfaust](https://mapwindow.discourse.group/u/jerryfaust)\
**Post date:** [March 30, 2020, 4:16pm UTC](https://mapwindow.discourse.group/t/getrelatedshapes-function-always-return-false/518/6 "2020-03-30T16:16:02Z")

</div>

Hello @Windground

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.

Regards,  
Jerry.

---

<div class="post-metadata">

**Author:** ![Windground](https://avatars.discourse-cdn.com/v4/letter/w/9fc348/32.png) [@Windground](https://mapwindow.discourse.group/u/Windground)\
**Post date:** [March 30, 2020, 5:18pm UTC](https://mapwindow.discourse.group/t/getrelatedshapes-function-always-return-false/518/7 "2020-03-30T17:18:07Z")

</div>

Good evening! Alright.

Sorry, can’t upload attachments.  
 ![image](https://global.discourse-cdn.com/free1/uploads/mapwindow1/original/1X/cdcd2eb3a1706c57f7696c7863dd8df2a6f2466b.png)

Here’s the link to the files.

> **[shapes.zip](https://drive.google.com/file/d/1XYZYy99x6QKi9XqGk605A2JvNh5Ei0vo/view?usp=sharing)**
>
> Google Drive file.

---

<div class="post-metadata">

**Author:** ![jerryfaust](https://yyz2.discourse-cdn.com/free1/user_avatar/mapwindow.discourse.group/jerryfaust/32/378_2.png) [@jerryfaust](https://mapwindow.discourse.group/u/jerryfaust)\
**Post date:** [March 30, 2020, 7:42pm UTC](https://mapwindow.discourse.group/t/getrelatedshapes-function-always-return-false/518/8 "2020-03-30T19:42:47Z")

</div>

Hello @Windground

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.

1. Perhaps you could strip it down to the bare minimum to verify if it works, then build back from there?
2. Or if you don’t mind posting your entire app, I could look a little deeper? I’d like to help if I can.

Respectfully,  
Jerry.

 ![image](https://global.discourse-cdn.com/free1/uploads/mapwindow1/original/1X/8b8eb283156c2139dc481e0d7f7d290d0c97e39a.png)

---

<div class="post-metadata">

**Author:** ![Windground](https://avatars.discourse-cdn.com/v4/letter/w/9fc348/32.png) [@Windground](https://mapwindow.discourse.group/u/Windground)\
**Post date:** [March 31, 2020, 6:34am UTC](https://mapwindow.discourse.group/t/getrelatedshapes-function-always-return-false/518/9 "2020-03-31T06:34:18Z")

</div>

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.

 ![final](https://global.discourse-cdn.com/free1/uploads/mapwindow1/original/1X/92bce95e0b2463d6f37807ff229d7b0b9978d385.png)

I came across this before, but here I did not expect to see this. Perhaps this should be taken into account in other functions that return values.

---

<div class="post-metadata">

**Author:** ![jerryfaust](https://yyz2.discourse-cdn.com/free1/user_avatar/mapwindow.discourse.group/jerryfaust/32/378_2.png) [@jerryfaust](https://mapwindow.discourse.group/u/jerryfaust)\
**Post date:** [March 31, 2020, 7:06am UTC](https://mapwindow.discourse.group/t/getrelatedshapes-function-always-return-false/518/10 "2020-03-31T07:06:37Z")

</div>

Hello @Windground, glad you got it worked out.

I don’t use c# as much as I do [VB.NET](http://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](https://www.mapwindow.org/documentation/mapwingis4.9/group __shapefile__ geoprocessing.html#ga1a0f9f0c07d9a5f4965e69070fdb4568). It does indicate that **Result** is a **ref** erence object.

![image](https://global.discourse-cdn.com/free1/uploads/mapwindow1/original/1X/a1929aa51bb4e06618bb9dedd7816267e588e0c8.png)

Regards,  
Jerry.

---

<div class="post-metadata">

**Author:** ![pmeems](https://yyz2.discourse-cdn.com/free1/user_avatar/mapwindow.discourse.group/pmeems/32/9_2.png) [@pmeems](https://mapwindow.discourse.group/u/pmeems)\
**Post date:** [January 5, 2022, 3:09pm UTC](https://mapwindow.discourse.group/t/getrelatedshapes-function-always-return-false/518/11 "2022-01-05T15:09:01Z")

</div>

Closed on January 5, 2022, 3:09pm UTC


