2

I don't understand the difference between OGR and Geopandas librairies. I have checked the documentation of GDAL as well as http://geopandas.org/ but I still don't get it. I just started learning about them and for now I'm trying to write some scripts in python using Geopandas.

My goal is to select features from a shapefile where field="value", then export the selected features to a shapefile. The second step would be to add another field to the output shapefile(with only the selected features from the first shapefile) and set it to a certain value. Doing the second part with OGR seems easy. This is how I've done it :

from osgeo import ogr
driver = ogr.GetDriverByName('ESRI Shapefile')
dataSrc = driver.Open("myshp.shp", 1)
fld = ogr.FieldDefn('City', ogr.OFTString)
lyr = dataSrc.GetLayer()
lyr.CreateField(fld)
for feature in lyr:
    feature.SetField("Not Found", fld)
    layer.SetFeature(feature)
dataSrc= None

However I don't see how I can do the same thing using Geopandas. I might be missing the point though that's why I'm confused between the use of the two librairies.

I know that this is easily done using QGIS or ArcGIS manually, but as of now I'm trying to learn how to use python with Geopandas.

How can I achieve my goal explained above using Geopandas?

  • I think you should focus your question on how to achieve that one specific goal instead of asking a broad question about differences. – PolyGeo Jul 19 '16 at 20:56
  • @PolyGeo My main concern is to know the use of each library and therefore the difference between them. By knowing that I might know if that goal is achievable in the first place or not. I don't know how specific I can be? – GeoSal Jul 20 '16 at 0:14
  • 1
    I've edited to show how. It now asks a single specific question rather than two questions (one specific and one broad). – PolyGeo Jul 20 '16 at 0:18
2

I didn't knew geopandas, it is very easy to add a field, a great library indeed. You just need to read your file add it and save.

dataSrc = gpd.read_file('my_shp.shp')
dataSrc['new_field'] = 1
dataSrc.to_file('newfile.shp')

Selecting is also straightforward:

dataSrc = gpd.read_file('my_shp.shp')
dataSrc[dataSrc['id']<4].to_file('new_shape.shp')
  • Thank u so much @caiohamamura! It works. I am still confused though about the use of OGR and Geopandas. Can u clarify this point for me please? – GeoSal Jul 20 '16 at 1:23
  • 1
    They are just different libraries, as I said I don't know geopandas, looks like a new library (version is still 0.2). OGR is very mature. I think geopandas still won't have many options to access things OGR already does, but for what I've already seen geopandas is FAR easier. But usually there's a tradeoff between easy and access to advanced features. – caiohamamura Jul 20 '16 at 1:26
  • 2
    Also another difference is that OGR is directly wrapped to C++ OGR libraries, while geopandas uses other python libraries such as fiona which uses OGR wrapper, so you have many layers between python code and C++ code, this usually will lead to worse performance. – caiohamamura Jul 20 '16 at 1:31

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.