Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python to convert csv files (point table and line table) into shapefile files

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to use Python to convert csv files (point tables and line tables) into shapefile files." interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to use Python to convert csv files (point tables and line tables) into shapefile files.

Python implements the conversion of csv files (point table and line table) to shapefile files

Description

The geometry coordinate used in the point table is wkbPoint (geometric point coordinate).

The geometry coordinate used by the line table is wkbLineString (multipoint coordinates)

See the screenshot for the difference.

Add comments with reference to the methods written by others, write the conversion of points and edges into functions, and add reading shp files after each successful conversion to test whether the conversion is successful or not

1. Solve the Chinese coding of shp dbf files

# gdal.SetConfigOption ("SHAPE_ENCODING", "")

# gdal.SetConfigOption ("SHAPE_ENCODING", "gb2312")

Gdal.SetConfigOption ("SHAPE_ENCODING", "gbk")

2.layer.CreateField (ogr.FieldDefn ('local_id', ogr.OFTString))

The name of the created field should be in English, and it should not exceed the cross character. One Chinese character accounts for more than one character.

Code

(geopandas gdal import should be considered in order, otherwise an error will be reported.)

Import os, reimport geopandas as gpdimport gdalimport pandas as pdfrom osgeo import ogr, osr # osr are used to obtain coordinate system Ogr is used to process vector files from conf.path_config import gis_excel_dir# to solve the Chinese code of shp dbf files selected from the commonly used gbk gb2312 utf8# gdal.SetConfigOption ("SHAPE_ENCODING", ") # gdal.SetConfigOption (" SHAPE_ENCODING "," gb2312 ") gdal.SetConfigOption (" SHAPE_ENCODING "," gbk ") def point_csv_2_shp (path) Shp_fn): "" point to shp "" os.chdir (os.path.dirname (path)) # sets the directory where path is located to the current folder ds = ogr.Open (path, 1) # 1 represents readable and writable Default is 0 csv_lyr = ds.GetLayer () # get csv file sr = osr.SpatialReference () sr.ImportFromEPSG (4326) # define coordinate system shp_driver = ogr.GetDriverByName ('ESRI Shapefile') # get shapefile file handling handle if os.path.exists (shp_fn): # delete shp_driver.DeleteDataSource (shp) if the file with the same name already exists in the folder _ fn) shp_ds = shp_driver.CreateDataSource (shp_fn) layer = shp_ds.CreateLayer (shp_fn) Sr, ogr.wkbPoint) # create a point layer layer.CreateField (ogr.FieldDefn ('id', ogr.OFTString)) layer.CreateField (ogr.FieldDefn (' ground_h', ogr.OFTString)) layer.CreateField (ogr.FieldDefn ('type') Ogr.OFTString) for csv_row in csv_lyr: # for each line in the csv file point_feature = ogr.Feature (layer.GetLayerDefn ()) # create a point x = csv_row.GetFieldAsDouble ('Abscissa') # csv coordinate field y = csv_row.GetFieldAsDouble ('ordinate') # csv coordinate field shp_pt = ogr.Geometry ( Ogr.wkbPoint) # create a geometric point shp_pt.AddPoint (x Y) # get csv field # assign point_feature.SetField ('id', csv_row.GetFieldAsString (' local_id')) to the created shp file field # GetFieldAsString get the column name point_feature.SetField ('ground_h', csv_row.GetFieldAsString (' ground_h')) point_feature.SetField ('type') in csv Csv_row.GetFieldAsString ('dev_type')) point_feature.SetGeometry (shp_pt) # add geometric data of points to points layer.CreateFeature (point_feature) # write points to layers del ds del shp_ds # release handle Files are buffered to disk print ("This process has succeeded!") def line_csv_2_dbf (path, shp_fn): "" geometry coordinates are LineString coordinates "" os.chdir (os.path.dirname (path)) # sets the directory where path is located to the current folder ds = ogr.Open (path, 1) # 1 represents readable and writable Default is 0 csv_lyr = ds.GetLayer () # get csv file sr = osr.SpatialReference () sr.ImportFromEPSG (4326) # define coordinate system shp_driver = ogr.GetDriverByName ('ESRI Shapefile') # get shapefile file handling handle if os.path.exists (shp_fn): # delete shp_driver.DeleteDataSource (shp) if the file with the same name already exists in the folder _ fn) shp_ds = shp_driver.CreateDataSource (shp_fn) layer = shp_ds.CreateLayer (shp_fn) Sr, ogr.wkbLineString) # create multiple point layers: layer.CreateField (ogr.FieldDefn ('id', ogr.OFTString)) layer.CreateField (ogr.FieldDefn (' id', ogr.OFTString), ogr.OFTString)) layer.CreateField (ogr.FieldDefn ('utilid`) Ogr.OFTString)) for csv_row in csv_lyr: # for each line in the csv file point_feature = ogr.Feature (layer.GetLayerDefn ()) x1 = csv_row.GetFieldAsDouble ('x1') # csv x1 coordinate y1 = csv_row.GetFieldAsDouble (' y1') # y1 coordinate x2 = csv_row.GetFieldAsDouble ('x2') # x1 coordinate y2 = csv_ Row.GetFieldAsDouble ('y2') # y2 coordinates mult_coord =' ('+ str (x1) +'+ str (y1) +' '+ str (x2) +' + str (y2) +')'# geom = ogr.CreateGeometryFromWkt ('LINESTRING' +' (21 csv 01)') geom = ogr.CreateGeometryFromWkt ('LINESTRING' + mult_coord) # get csv field # assign point_feature.SetField (' id') to the created shp file field Csv_row.GetFieldAsString ('gid')) # GetFieldAsString gets the column name point_feature.SetField (' literidcharacters, csv_row.GetFieldAsString ('literidaries')) point_feature.SetField ('upriidcharacters, csv_row.GetFieldAsString (' utilisidals') point_feature.SetGeometryDirectly (geom) layer.CreateFeature (point_feature) del ds del shp_ds # release handle in csv File buffered to disk print ("This process has succeeded!") def read_shapefile (path): "Test converted shp file" df = gpd.read_file (path, encoding='gbk') " Rows=20) # Encoding format print (df) if _ _ name__ = ='_ _ main__': 'line table to shp and reading test' 'shp_fn = "gd.shp" # the final shp file name path= os.path.join (gis_excel_dir,' line.csv') # csv file name line_csv_2_dbf (path=path) Shp_fn=shp_fn) # read test result read_shapefile (path=os.path.join (gis_excel_dir, 'gd.shp'))' 'point table to shp and read test' # shp_fn= "xnd.shp" # path=os.path.join (gis_excel_dir, 'point.csv') # csv file name # point_csv_2_shp (path=path Shp_fn=shp_fn) # read the result of the test transfer # read_shapefile (path=os.path.join (gis_excel_dir, 'point.shp')) here I believe that you have a deeper understanding of "how to use Python to convert csv files (point tables and line tables) into shapefile files". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report