Understand shapefiles in different projections (notably OSGB)
[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
34                         if (shp.shapeType==ShpType.SHAPE_POLYGON || shp.shapeType==ShpType.SHAPE_POLYLINE) {
35
36                                 // Loop through all polylines in the shape
37                                 var polyArray:Array = ShpTools.readRecords(files[0]);
38                                 for (var i:uint=0; i<polyArray.length; i++) {
39
40                                         // Get attributes like this:
41                                         //              var dr:DbfRecord = DbfTools.getRecord(files[2], dbf, i);
42                                         //              var xsID:String = dr.values[idFieldName];
43
44                                         // Do each ring in turn, then each point in the ring
45                                         var way:Way;
46                                         var node:Node;
47                                         var x:Number, y:Number;
48                                         for (var j:int=0; j < polyArray[i].shape.rings.length; j++) {
49                                                 var nodestring:Array=[];
50                                                 var points:Array = polyArray[i].shape.rings[j];
51                                                 if (points!=null) {
52                                                         for (var k:int=0; k < points.length; k++) {
53                                                                 var p:ShpPoint = ShpPoint(points[k]);
54                                                                 
55                                                                 if (projection) {
56                                                                         var r:ProjPoint = new ProjPoint(p.x,p.y,0);
57                                                                         r=proj.transform(fromProj,toProj,r);
58                                                                         x=r.x; y=r.y;
59                                                                 } else {
60                                                                         x=p.x; x=p.y;
61                                                                 }
62
63                                                                 var key:String=x+","+y;
64                                                                 if (nodemap[key]) { node=nodemap[key]; }
65                                                                 else { node=connection.createNode({}, y, x, push); nodemap[key]=node; }
66                                                                 nodestring.push(node);
67                                                         }
68                                                 }
69                                                 if (nodestring.length>0) {
70                                                         way=connection.createWay({}, nodestring, push);
71                                                         if (simplify) { Simplify.simplify(way, map, false); }
72                                                 }
73                                         }
74                                 }
75                         }
76                 }
77
78         }
79 }