@@ -24,33 +24,41 @@ public function newEloquentBuilder($query)
2424 return new Builder ($ query );
2525 }
2626
27+ protected function geogFromText (GeometryInterface $ geometry )
28+ {
29+ return $ this ->getConnection ()->raw (sprintf ("%s.ST_GeogFromText('%s') " ,
30+ function_exists ('config ' ) ? config ('postgis.schema ' ) : 'public ' , $ geometry ->toWKT ()));
31+ }
32+
33+ protected function geomFromText (GeometryInterface $ geometry , $ srid = 4326 )
34+ {
35+ return $ this ->getConnection ()->raw (sprintf ("%s.ST_GeomFromText('%s', '%d') " ,
36+ function_exists ('config ' ) ? config ('postgis.schema ' ) : 'public ' , $ geometry ->toWKT (), $ srid ));
37+ }
38+
39+ public function asWKT (GeometryInterface $ geometry , $ attrs )
40+ {
41+ switch (strtoupper ($ attrs ['geomtype ' ])) {
42+ case 'GEOMETRY ' :
43+ return $ this ->geomFromText ($ geometry , $ attrs ['srid ' ]);
44+ break ;
45+ case 'GEOGRAPHY ' :
46+ default :
47+ return $ this ->geogFromText ($ geometry );
48+ break ;
49+ }
50+ }
51+
2752 protected function performInsert (EloquentBuilder $ query , array $ options = [])
2853 {
2954 foreach ($ this ->attributes as $ key => $ value ) {
3055 if ($ value instanceof GeometryInterface) {
3156 $ this ->geometries [$ key ] = $ value ; //Preserve the geometry objects prior to the insert
3257 if (! $ value instanceof GeometryCollection) {
3358 $ attrs = $ this ->getPostgisType ($ key );
34- switch (strtoupper ($ attrs ['geomtype ' ])) {
35- case 'GEOMETRY ' :
36- $ this ->attributes [$ key ] = $ this ->getConnection ()->raw (
37- sprintf ("%s.ST_GeomFromText('%s', '%d') " ,
38- function_exists ('config ' ) ? config ('postgis.schema ' ) : 'public ' , $ value ->toWKT (), $ attrs ['srid ' ])
39- );
40- break ;
41- case 'GEOGRAPHY ' :
42- default :
43- $ this ->attributes [$ key ] = $ this ->getConnection ()->raw (
44- sprintf ("%s.ST_GeogFromText('%s') " ,
45- function_exists ('config ' ) ? config ('postgis.schema ' ) : 'public ' , $ value ->toWKT ())
46- );
47- break ;
48- }
59+ $ this ->attributes [$ key ] = $ this ->asWKT ($ value , $ attrs );
4960 } else {
50- $ this ->attributes [$ key ] = $ this ->getConnection ()->raw (
51- sprintf ("%s.ST_GeomFromText('%s', 4326) " ,
52- function_exists ('config ' ) ? config ('postgis.schema ' ) : 'public ' , $ value ->toWKT ())
53- );
61+ $ this ->attributes [$ key ] = $ this ->geomFromText ($ value );
5462 }
5563 }
5664 }
0 commit comments