4

I want to convert a shapefile from polygon to point:

pfile = pathtofile
file = gpd.read_file(pfile) #it is a polygon

Searched this site but didn't find anything similar: http://geopandas.org/geometric_manipulations.html

  • Try GeoSeries.centroid. Basically what you have to do is calculate the centroid of the polygon and then store it into another variable. – Ahsan Mukhtar Nov 13 '18 at 10:27
  • 1
    Please Edit the question to specify if you want one point, a multipoint, or many points. – Vince Nov 13 '18 at 11:45
6

You can use the centroid of the polygons as Ahsan Mukhtar says

The polygons

import geopandas as gpd
polys = gpd.read_file("polygons.shp")
polys.head()
       POLY_NUM  Zn  Pb                         geometry
 0          24  10   0  POLYGON ((-716632.5126561673 -954202.638843462
 1          25  30  34  POLYGON ((-716623.8860221611 -954167.954016855
 2          26   5  55  POLYGON ((-716669.4794148145 -954153.082579828
 3          27  50  30  POLYGON ((-716573.0498599194 -954131.716264446
 4          28  80  67  POLYGON ((-716600.4004366234 -954260.332832906

enter image description here

Centroids of the polygons

# copy GeoDataFrame
points = polys.copy()
# change geometry 
points['geometry'] = points['geometry'].centroid
points.head()
       POLY_NUM  Zn  Pb                        geometry
 0          24  10   0  POINT (-716588.6708054555 -954243.4902445012)
 1          25  30  34  POINT (-716569.9936684616 -954200.8100592281)
 2          26   5  55  POINT (-716657.9447893198 -954169.1795523901)
 3          27  50  30  POINT (-716546.9534786827 -954140.7322584245)
 4          28  80  67  POINT (-716581.1026726362 -954286.1981425789)

enter image description here

But you can also use the exterior coordinates of the polygons to obtain MultiPoints as Vince says

from shapely import MultiPoints
► correct minor mistakes from shapely.geometry import MultiPoint
points2 = polys.copy()
points2.geometry = points2.geometry.apply(lambda x: MultiPoint(list(x.exterior.coords)))
points2.head()
       POLY_NUM  Zn  Pb                        geometry
 0          24  10   0  (POINT (-716632.5126561673 -954202.6388434628)...
 1          25  30  34  (POINT (-716623.8860221611 -954167.954016855),...
 2          26   5  55  (POINT (-716669.4794148145 -954153.0825798288)...
 3          27  50  30  (POINT (-716573.0498599194 -954131.7162644463)...
 4          28  80  67  (POINT (-716600.4004366234 -954260.3328329064)...

enter image description here

You can also split the exterior coordinates into multiples points. For that you need a new GeoDataFrame (more points than polygons)

from shapely import Point
col = polys.columns.tolist()
print(col)
[u'POLY_NUM_B', u'Zn', u'Pb', 'geometry']
# new GeoDataFrame with same columns
nodes = gpd.GeoDataFrame(columns=col)
# Extraction of the polygon nodes and attributes values from polys and integration into the new GeoDataFrame
for index, row in polys.iterrows():
    for j in list(row['geometry'].exterior.coords): 
        nodes = nodes.append({'POLY_NUM': int(row['POLY_NUM']), 'Pb':row['Pb'],'Zn':row['Zn'], 'geometry':Point(j) },ignore_index=True)
nodes.head()
       POLY_NUM  Zn  Pb                        geometry
 0         24  10  0  POINT (-716632.5126561673 -954202.6388434628)
 1         24  10  0   POINT (-716599.344199169 -954224.8253932064)
 2         24  10  0  POINT (-716538.7964301996 -954240.9772354551)
 3         24  10  0  POINT (-716529.3520701742 -954273.9782994529)
 4         24  10  0  POINT (-716550.9210327187 -954269.8358586552)

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.