jonas-p / go-shp

Go library for reading and writing ESRI Shapefiles. Pure Golang implementation based on the ESRI Shapefile technical description.
MIT License
258 stars 68 forks source link

Read shapfile with Multipolygon #44

Open gonponlotus opened 4 years ago

gonponlotus commented 4 years ago

Hi everyone, how can i read shapefile with the geom is multipolygon and convert to array list polygon

thank you

wangxin0814 commented 4 years ago

geomentry := (metaData.Shape).(*goShp.Polygon)

            coords := [][]geom.Coord{}
            for i := int32(0); i < geomentry.NumParts; i++ {
                coord := []geom.Coord{}

                if i == 0 {
                    if geomentry.NumParts > 1 {
                        for _, point := range geomentry.Points[0:geomentry.Parts[i+1]] {
                            coord = append(coord, []float64{point.X, point.Y})
                        }
                        coords = append(coords, coord)
                    } else { // 只有一个部分
                        for _, point := range geomentry.Points[0:] {
                            coord = append(coord, []float64{point.X, point.Y})
                        }
                        coords = append(coords, coord)
                    }
                } else if i == geomentry.NumParts-1 {
                    for _, point := range geomentry.Points[geomentry.Parts[i]:] {
                        coord = append(coord, []float64{point.X, point.Y})
                    }
                    coords = append(coords, coord)
                } else {
                    for _, point := range geomentry.Points[geomentry.Parts[i]:geomentry.Parts[i+1]] {
                        coord = append(coord, []float64{point.X, point.Y})
                    }
                    coords = append(coords, coord)
                }
            }

            polygon := geom.NewPolygon(geom.XY).MustSetCoords(coords).SetSRID(4326)