dd0cf6cee1a021af090aa35c9e5579dd34119d20
[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 com.gradoservice.proj4as.*;
6         import net.systemeD.halcyon.Map;
7         import net.systemeD.halcyon.connection.Connection;
8         import net.systemeD.halcyon.connection.Node;
9         import net.systemeD.halcyon.connection.Way;
10         import net.systemeD.potlatch2.tools.Simplify;
11
12         public class ShpImporter extends Importer {
13
14                 private var projection:String;
15
16                 public function ShpImporter(connection:Connection, map:Map, filenames:Array, callback:Function=null, simplify:Boolean=false, projection:String="") {
17                         if (projection!='') this.projection=projection;
18                         super(connection,map,filenames,callback,simplify);
19                 }
20
21                 override protected function doImport(push:Function): void {
22                         // we load .shp as files[0], .shx as files[1], .dbf as files[2]
23                         var shp:ShpHeader=new ShpHeader(files[0]);
24                         var dbf:DbfHeader=new DbfHeader(files[2]);
25
26                         if (projection) {
27                                 var proj:Proj4as=new Proj4as();
28                                 var toProj:ProjProjection=new ProjProjection('EPSG:4326');
29                                 var fromProj:ProjProjection=new ProjProjection('EPSG:27700');
30                         }
31
32                         var nodemap:Object={};
33                         var key:String, v:String;
34
35                         if (shp.shapeType==ShpType.SHAPE_POLYGON || shp.shapeType==ShpType.SHAPE_POLYLINE) {
36
37                                 // Loop through all polylines in the shape
38                                 var polyArray:Array = ShpTools.readRecords(files[0]);
39                                 for (var i:uint=0; i<polyArray.length; i++) {
40
41                                         // Get attributes and create a tags hash
42                                         // (note that dr.values is a Dictionary)
43                                         var dr:DbfRecord = DbfTools.getRecord(files[2], dbf, i);
44                                         var tags:Object={};
45                                         for (key in dr.values) {
46                                                 v=dr.values[key];
47                                                 while (v.substr(v.length-1,1)==" ") v=v.substr(0,v.length-1);
48                                                 while (v.substr(0,1)==" ") v=v.substr(1);
49                                                 if (v!='') tags[key.toLowerCase()]=v;
50                                         }
51
52                                         // Do each ring in turn, then each point in the ring
53                                         var way:Way;
54                                         var node:Node;
55                                         var x:Number, y:Number;
56                                         for (var j:int=0; j < polyArray[i].shape.rings.length; j++) {
57                                                 var nodestring:Array=[];
58                                                 var points:Array = polyArray[i].shape.rings[j];
59                                                 if (points!=null) {
60                                                         for (var k:int=0; k < points.length; k++) {
61                                                                 var p:ShpPoint = ShpPoint(points[k]);
62                                                                 
63                                                                 if (projection) {
64                                                                         var r:ProjPoint = new ProjPoint(p.x,p.y,0);
65                                                                         r=proj.transform(fromProj,toProj,r);
66                                                                         x=r.x; y=r.y;
67                                                                 } else {
68                                                                         x=p.x; y=p.y;
69                                                                 }
70
71                                                                 key=x+","+y;
72                                                                 if (nodemap[key]) { node=nodemap[key]; }
73                                                                 else { node=connection.createNode({}, y, x, push); nodemap[key]=node; }
74                                                                 nodestring.push(node);
75                                                         }
76                                                 }
77                                                 if (nodestring.length>0) {
78                                                         way=connection.createWay(tags, nodestring, push);
79                                                         if (simplify) { Simplify.simplify(way, map, false); }
80                                                 }
81                                         }
82                                 }
83                         }
84                 }
85
86         }
87 }