4

I want to get geometry type for every row (like point, linestring, polygon etc.) from GeoJSON data.

This is my code for reading data.

import argparse
import geopandas as gpd
import os
import sys
import shapely

data = gpd.read_file('data.geojson')
for index, row in data.iterrows():
    print('Data: ', row['geometry'])

I have tried these solutions. But con not works. Get a Shapefile Geometry Type

Do you have any idea?

This is my 'data.geojson' file.

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [102.0, 0.5]
      },
      "properties": {
        "prop0": "value0"
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "LineString",
        "coordinates": [
          [102.0, 0.0], [103.0, 1.0], [104.0, 0.0], [105.0, 1.0]
        ]
      },
      "properties": {
        "prop0": "value0",
        "prop1": 0.0
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [100.0, 0.0], [101.0, 0.0], [101.0, 1.0],
            [100.0, 1.0], [100.0, 0.0]
          ]
        ]
      },
      "properties": {
        "prop0": "value0",
        "prop1": { "this": "that" }
      }
    }
  ]
}

1 Answer 1

3

The GeoSeries class in geopandas package has geom_type attribute which gives you type of features geometry. Please review link: Overview of Attributes and Methods.

import geopandas as gpd

data = gpd.read_file('data.geojson')
data.geom_type # Here, geom_type returns GeoSeries

# OUTPUT:
# 0         Point
# 1    LineString
# 2       Polygon
# dtype: object

Or you can use geom_type attribute to get type of any geometry in shapely package.

import geopandas as gpd

data = gpd.read_file('data.geojson')
for index, row in data.iterrows():
    print(row['geometry'].geom_type) # geom_type returns string, like 'Point' etc.

# OUTPUT:
# Point
# LineString
# Polygon
1
  • Thank you very much Dear @Kadir Şahbaz. The second method best for me. Thank you again :)
    – fdas
    Commented Jun 25, 2019 at 14:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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