Shapemerge function always returns null

I use the shapemerge function of utils to fuse two shapes in a ShapeFile,Shapemerge function always returns null,this is my code:

                m_ut.CreateInstance(__uuidof(::Utils));              
                IShapePtr shp;
		shp.CreateInstance(__uuidof(::Shape));
		shp->Create(ShpfileType::SHP_POLYGON);
		shp->AddPoint(lon[0], lat[0]);
		shp->AddPoint(lon[1], lat[1]);
		shp->AddPoint(lon[2], lat[2]);
		shp->AddPoint(lon[3], lat[3]);
		m_sf->EditAddShape(shp);
		int n = m_sf->GetNumShapes();
		if (n == 2)
		{
			IShapePtr shp2;
			shp2.CreateInstance(__uuidof(::Shape));
			shp2->Create(ShpfileType::SHP_POLYGON);
			shp2 = m_ut->ShapeMerge(m_sf, 0, 1, NULL);
			m_sf->EditAddShape(shp2);
		}

Hello @zcw

So I looked up the method in the source code, and it’s not good news. It’s not been implemented.

STDMETHODIMP CUtils::ShapeMerge(IShapefile *Shapes, long IndexOne, long IndexTwo, ICallback *cBack, IShape **retval)
{
    AFX_MANAGE_STATE(AfxGetStaticModuleState())
    ErrorMessage(tkMETHOD_NOT_IMPLEMENTED);
    return S_OK;
}

You could create a multi-part shape yourself by iterating the points of each shape and adding them to the new shape. It would not be very difficult.

Regards,
Jerry.

Thank you for your help @jerryfaust :smiley: . How to build a multi-part shape? Do you mean to add all points into one shape? Different shapes may intersect. I want to get the outline of all shapes

Hello @zcw

Ok. What I think you want is a Union operation. Unfortunately, at this point, the only option through MapWinGIS is the union of 2 shapefiles (Shapefile.Union). This is not too difficult. You can add one shape to one in-memory shapefile, and the other shape to a second shapefile. Then call the Union function. Presuming that the shapes overlap, then the returned shapefile should contain a single shape, being the union of the two shapes.

Sorry there’s not an easier way.

Regards,
Jerry.

Think you @jerryfaust. I changed the UNION function, but it returns NULL,this is my code:

IShapefilePtr sf1;
sf1.CreateInstance(__uuidof(::Shapefile));
sf1->CreateNew("", ShpfileType::SHP_POLYGON);
IShapePtr shp1;
shp1.CreateInstance(__uuidof(::Shape));
shp1->Create(ShpfileType::SHP_POLYGON);
shp1->AddPoint(110.05784, 34.58372);
shp1->AddPoint(110.07784, 34.59372);
shp1->AddPoint(110.07784, 34.60372);
shp1->AddPoint(110.06784, 34.60372);
sf1->EditAddShape(shp1);

IShapefilePtr sf2;
sf2.CreateInstance(__uuidof(::Shapefile));
sf2->CreateNew("", ShpfileType::SHP_POLYGON);
IShapePtr shp2;
shp2.CreateInstance(__uuidof(::Shape));
shp2->Create(ShpfileType::SHP_POLYGON);
shp2->AddPoint(110.06284, 34.58872);
shp2->AddPoint(110.08284, 34.59872);
shp2->AddPoint(110.08284, 34.60872);
shp2->AddPoint(110.07284, 34.60872);
sf2->EditAddShape(shp2);

IShapefilePtr sf3;
sf3 = sf1->Union(0, sf2, 0);
m_pGM->m_WinMap.AddLayer(sf3, true);

Hello @zcwbnu

Try closing the polygons (adding a fifth point that is identical to the first). See if that does it.

Thank you for your reply@jerryfaust. It doesn’t work.

Ok. I’ll try entering the code and see what happens.

I’m sorry @zcwbnu , it’s late here, and I’ll have to get back to this tomorrow.

The points in the geometries were being added counter-clockwise, so the polygons were invalid. Just reverse the entry of the points, and as before, make sure to close the polygons. You can verify by calling IsValid and IsValidReason.

Even so, although I can see the two geometries, and their overlap, I am getting a null shapefile returned (effectively because it says the Union is returning no shapes). I don’t yet know why, and I’ll look more tomorrow.

Regards,
Jerry.

@zcwbnu Do you still have this issue?