Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ElmerCSC
GitHub Repository: ElmerCSC/elmerfem
Path: blob/devel/elmerice/Meshers/GIS/MeshToShp.py
3206 views
1
#!/usr/bin/python
2
#==========================================
3
#
4
# FILE: MeshToShp.py
5
# USAGE : python MeshToShp.py [-h] -d <inputdir>
6
# DESCRIPTION: Create shapefiles from elmer mesh
7
#
8
# BUGS: ---
9
#
10
# AUTHOR: F. Gillet-Chaulet
11
# ORGANIZATION: IGE(CNRS-France)
12
#
13
# VERSION: V1
14
# CREATED: 2020-05-02
15
# MODIFIED:
16
#
17
#==========================================
18
import sys, getopt,os
19
20
def main(argv):
21
import shapefile
22
23
found_d=False
24
bc_only=False
25
try:
26
opts, args = getopt.getopt(argv,"hd:",["bc"])
27
except getopt.GetoptError:
28
usage()
29
sys.exit(2)
30
for opt, arg in opts:
31
if opt == '-h':
32
usage()
33
sys.exit()
34
elif opt in ("-d"):
35
dir_name= arg
36
found_d=True
37
elif opt in ("--bc"):
38
bc_only=True
39
40
if not found_d:
41
print('missing mandatory mesh dir name')
42
usage()
43
sys.exit()
44
45
nodefile=os.path.join(dir_name, 'mesh.nodes')
46
vertices = {}
47
with open(nodefile) as fin:
48
for line in fin:
49
(j,k,x,y,z) = line.split()
50
vertices[int(j)] = [float(x), float(y)]
51
52
outputdir="{}_shp".format(dir_name)
53
try:
54
# Create target Directory
55
os.makedirs(outputdir,exist_ok=True)
56
57
except OSError as error:
58
print("Directory {} can not be created" .format(outputdir))
59
60
## bc as polylines
61
bcfile=os.path.join(outputdir, 'boundaries')
62
shp=shapefile.Writer(bcfile, shapefile.POLYLINE)
63
shp.field('enum', 'N')
64
shp.field('etype', 'N')
65
shp.field('BCId', 'N')
66
67
boundaryfile = os.path.join(dir_name, 'mesh.boundary')
68
with open(boundaryfile) as fin:
69
for line in fin:
70
l = line.split()
71
enum,bc,etype=int(l[0]),int(l[1]),int(l[4])
72
nv=etype%100
73
evertices = [vertices[int(i)] for i in l[5:5+nv]]
74
shp.line([evertices])
75
shp.record(enum,etype,bc)
76
77
shp.close()
78
79
if not bc_only :
80
## elements as polygons
81
bcfile=os.path.join(outputdir, 'elements')
82
shp=shapefile.Writer(bcfile, shapefile.POLYGON)
83
shp.field('enum', 'N')
84
shp.field('etype', 'N')
85
shp.field('BodyId', 'N')
86
87
Efile = os.path.join(dir_name, 'mesh.elements')
88
with open(Efile) as fin:
89
for line in fin:
90
l = line.split()
91
enum,bd,etype=int(l[0]),int(l[1]),int(l[2])
92
nv=etype%100
93
evertices = [vertices[int(i)] for i in l[3:3+nv]]
94
95
# As its elements so no hole
96
# should we check the rotation order?
97
# seems not
98
# this would be the solution with shapely
99
#polygon = shapely.geometry.Polygon(evertices)
100
#if not polygon.exterior.is_ccw:
101
# evertices=evertices[::-1]
102
103
# spyshp autoimatically add lastpt=firstpt
104
# to close polygons
105
shp.poly([evertices])
106
shp.record(enum,etype,bd)
107
108
shp.close()
109
110
print("Shapefiles for Elmer mesh have been created")
111
print("You can define the projection with gdal tools")
112
print(" gdalsrsinfo -o wkt \"EPSG:XYZW\" > {}/elements.prj".format(outputdir))
113
print(" gdalsrsinfo -o wkt \"EPSG:XYZW\" > {}/boundaries.prj".format(outputdir))
114
115
def usage():
116
print('usage: MeshToShp.py [-h] -d <inputfile>')
117
print('options:')
118
print(' -h [print help]')
119
print(' -d <mesh dir. name>')
120
print(' --bc [output only boundary elements]')
121
122
123
if __name__ == "__main__":
124
main(sys.argv[1:])
125
126