friendlier name
[potlatch2.git] / net / systemeD / potlatch2 / utils / ShpImporter.as
1 package net.systemeD.potlatch2.utils {
2
3         import org.vanrijkom.shp.*;
4         import org.vanrijkom.dbf.*;
5         import net.systemeD.halcyon.MapPaint;
6         import net.systemeD.halcyon.Globals;
7         import net.systemeD.halcyon.connection.Node;
8         import net.systemeD.halcyon.connection.Way;
9         import net.systemeD.potlatch2.tools.Simplify;
10
11         public class ShpImporter extends Importer {
12
13                 public function ShpImporter(container:*, paint:MapPaint, filenames:Array, callback:Function=null, simplify:Boolean=false) {
14                         super(container,paint,filenames,callback,simplify);
15                 }
16
17                 override protected function doImport(): void {
18                         // we load .shp as files[0], .shx as files[1], .dbf as files[2]
19                         var shp:ShpHeader=new ShpHeader(files[0]);
20                         var dbf:DbfHeader=new DbfHeader(files[2]);
21
22                         if (shp.shapeType==ShpType.SHAPE_POLYGON || shp.shapeType==ShpType.SHAPE_POLYLINE) {
23
24                                 // Loop through all polylines in the shape
25                                 var polyArray:Array = ShpTools.readRecords(files[0]);
26                                 for (var i:uint=0; i<polyArray.length; i++) {
27
28                                         // Get attributes like this:
29                                         //              var dr:DbfRecord = DbfTools.getRecord(files[2], dbf, i);
30                                         //              var xsID:String = dr.values[idFieldName];
31
32                                         // Do each ring in turn, then each point in the ring
33                                         for (var j:int=0; j < polyArray[i].shape.rings.length; j++) {
34                                                 var way:Way;
35                                                 var nodestring:Array=[];
36                                                 var points:Array = polyArray[i].shape.rings[j];
37                                                 if (points!=null) {
38                                                         for (var k:int=0; k < points.length; k++) {
39                                                                 var p:ShpPoint = ShpPoint(points[k]);
40                                                                 nodestring.push(container.createNode({}, p.y, p.x));
41                                                         }
42                                                 }
43                                                 if (nodestring.length>0) {
44                                                         way=container.createWay({}, nodestring);
45                                                         if (simplify) { Simplify.simplify(way, paint.map, false); }
46                                                 }
47                                         }
48                                 }
49                         }
50                 }
51
52         }
53 }