77using ProjNet . CoordinateSystems ;
88using ProjNet . CoordinateSystems . Transformations ;
99
10-
1110namespace BIE . DataPipeline . Import
1211{
1312 internal class ShapeImporter : IImporter
@@ -19,7 +18,7 @@ internal class ShapeImporter : IImporter
1918
2019 public ShapeImporter ( DataSourceDescription ? dataSourceDescription )
2120 {
22- //YAML Arguments:
21+ // YAML Arguments:
2322 this . mDataSourceDescription = dataSourceDescription ;
2423
2524 new StringBuilder ( ) ;
@@ -46,17 +45,47 @@ public ShapeImporter(DataSourceDescription? dataSourceDescription)
4645
4746 private void SetupParser ( )
4847 {
49- // handle Zip file:
48+ MemoryStream shpStream = new MemoryStream ( ) ;
49+ MemoryStream dbfStream = new MemoryStream ( ) ;
50+
51+ if ( mDataSourceDescription . source . type . Equals ( "filepath" ) )
52+ {
53+ // handle local Zip file:
54+ Console . WriteLine ( $ "Opening local Zip file { mDataSourceDescription . source . location } ") ;
55+ using ( var zipStream = new FileStream ( mDataSourceDescription . source . location , FileMode . Open ) )
56+ {
57+ var zipArchive = new ZipArchive ( zipStream , ZipArchiveMode . Read ) ;
58+ ExtractShapeFilesFromZip ( zipArchive , shpStream , dbfStream ) ;
59+ }
60+ }
61+ else
62+ {
63+ // handle Zip file from URL:
64+ Console . WriteLine ( $ "Grabbing Webfile { mDataSourceDescription . source . location } ") ;
65+ var client = new HttpClient ( ) ;
66+ var zipStream = client . GetStreamAsync ( mDataSourceDescription . source . location ) . Result ;
67+
68+ Console . WriteLine ( "Opening the Zip file..." ) ;
69+ var zipArchive = new ZipArchive ( zipStream , ZipArchiveMode . Read ) ;
70+ ExtractShapeFilesFromZip ( zipArchive , shpStream , dbfStream ) ;
71+ }
5072
51- Console . WriteLine ( $ "Grabbing Webfile { mDataSourceDescription . source . location } ") ;
52- var client = new HttpClient ( ) ;
53- var zipStream = client . GetStreamAsync ( mDataSourceDescription . source . location ) . Result ;
73+ shpStream . Position = 0 ;
74+ dbfStream . Position = 0 ;
5475
55- Console . WriteLine ( "opening Zip file" ) ;
56- var zipArchive = new ZipArchive ( zipStream , ZipArchiveMode . Read ) ;
76+ mParser = Shapefile . CreateDataReader (
77+ new ShapefileStreamProviderRegistry (
78+ new ByteStreamProvider ( StreamTypes . Shape , shpStream ) ,
79+ new ByteStreamProvider ( StreamTypes . Data , dbfStream ) ,
80+ true ,
81+ true ) ,
82+ GeometryFactory . Default ) ;
5783
58- var shpStream = new MemoryStream ( ) ;
59- var dbfStream = new MemoryStream ( ) ;
84+ mHeader = mParser . DbaseHeader ;
85+ }
86+
87+ private void ExtractShapeFilesFromZip ( ZipArchive zipArchive , MemoryStream shpStream , MemoryStream dbfStream )
88+ {
6089 foreach ( var zipArchiveEntry in zipArchive . Entries )
6190 {
6291 if ( zipArchiveEntry . Name . EndsWith ( ".shp" ) )
@@ -73,34 +102,12 @@ private void SetupParser()
73102 }
74103 }
75104
76- if ( shpStream == null || dbfStream == null )
105+ if ( shpStream . Length == 0 || dbfStream . Length == 0 )
77106 {
78- throw new FileNotFoundException ( "the required .shp and .bdf files could not be found." ) ;
107+ throw new FileNotFoundException ( "The required .shp and .dbf files could not be found." ) ;
79108 }
80-
81- shpStream . Position = 0 ;
82- dbfStream . Position = 0 ;
83- Console . WriteLine ( "File loaded." ) ;
84-
85- mParser =
86- Shapefile . CreateDataReader (
87- new ShapefileStreamProviderRegistry (
88- new ByteStreamProvider (
89- StreamTypes . Shape ,
90- shpStream ) ,
91- new ByteStreamProvider (
92- StreamTypes . Data ,
93- dbfStream ) ,
94- true ,
95- true ) ,
96- GeometryFactory . Default ) ;
97-
98- // parser = new ShapefileDataReader(@"D:\datasets\093_Oberpfalz_Hausumringe\hausumringe",
99- // NetTopologySuite.Geometries.GeometryFactory.Default);
100- mHeader = mParser . DbaseHeader ;
101109 }
102110
103-
104111 /// <summary>
105112 /// reads the next line from the Shapefile and returns it as WKT (Well known text)
106113 /// </summary>
@@ -111,7 +118,7 @@ public bool ReadLine(out string nextLine)
111118 nextLine = "" ;
112119 if ( ! mParser . Read ( ) )
113120 {
114- Console . WriteLine ( "EOF" ) ;
121+ Console . WriteLine ( "Reached EOF, finishing. " ) ;
115122 return false ;
116123 }
117124
@@ -163,4 +170,4 @@ private Geometry ConvertUtmToLatLong(Geometry polygon)
163170 return polygon ;
164171 }
165172 }
166- }
173+ }
0 commit comments