[email protected]
2 years ago
136
2
Laravel

How to reference item in multi-dimensional array/collection

Posted 2 years ago by [email protected]

Hello Gang,

So really new to php and laravel but I am only using it to manage the backend of my leaflet data collection tool and cms. I need to pull my place data into leaflet which requires it in Geojson feature collection. So far I have the geojson feature collection format correct (output below) but I cannot retreive my converted geom into my Geojson. So far I have my transformed WKT Geom column (ex. POINT(-122.084047 37.422010))in postgis resulting in a multi-dimensional collection with "geometry": "{"type":"Point","coordinates":[-122.084047,37.42201]}" at the botton. Any help on this specific problem or how I could achieve the overall objective easier would be much appreciated. I would really like not have to read about walk recursive php but I think that is what I need.

I am basically trying to write Mcbribes PGSQL->Geojson->leaflet script so it can handle all geometry types

'''

class mapController extends Controller { /** * Create a new controller instance. * * @return void */

public function __construct() { $this->middleware('auth'); }

/**
 * Show the application dashboard.
 *
 * @return \Illuminate\Http\Response
 */

public function index() {

    $tekpoints = tekpoint::all();
    $contributors = contributor::pluck('firstName');
    $communities = community::pluck('name','id');

// query and get * from tekpoints // $get = DB::table('tekpoints')->select('*');

// Build Feature object Geojson for leaflet //

foreach( $tekpoints as $tekpoint) {

   $properties = array( 'Common Name' => "$tekpoint->tradPlaceName",'Traditional Name'  => "$tekpoint->tradPlaceName" , "Description" => "$tekpoint->description" , "First Hand" => "$tekpoint->firstHand","Second Hand" => "$tekpoint->secondHand", "Icon" => "$tekpoint->icon");
        
    
   $collection = $get->Select(DB::raw('ST_AsGeoJSON(geom) AS geometry'))->get();
       
dd($collection);

'''

    output:

Collection {#289 ▼ #items: array:2 [▼ 0 => {#291 ▼ +"geometry": "{"type":"Point","coordinates":[-122.084047,37.422009]}" } 1 => {#292 ▼ +"geometry": "{"type":"Point","coordinates":[-122.084047,37.42201]}" } ] }

'''

// try to pull geometry bolded portion from multi-dimensional array //

// method 1 I am not sure how to reference numbered array level //

   $geometry = array_get($collection, 'items.geometry');
      dd($geometry);

'''

    output:

    null

'''

// method 2 //

     $only = $collection->only(['$items.geometry']);
       $only->all();
        dd($only);

'''

    output: 

Collection {#290 ▼ #items: [] }

        $feature = array(
        "type" => 'Feature',
        "properties" => $properties,
        "geometry" =>  $geometry 
        );

// Build GeoJSON feature collection array // $geojson = array( 'type' => 'FeatureCollection', 'features' => $feature );

     $finalGeojson= json_encode($feature);
         dd($finalGeojson);

'''

output:

"{"type":"FeatureCollection","features":{"type":"Feature","properties":{"Common Name":"camp","Traditional Name":"camp","Description":"edfsfewf","First Hand":"csaefsaef","Second Hand":"scsefs","Icon":"edfadf"},"geometry":null}}"

''' class mapController extends Controller { /** * Create a new controller instance. * * @return void */ public function __construct() { $this->middleware('auth'); }

/**
 * Show the application dashboard.
 *
 * @return \Illuminate\Http\Response
 */
public function index()
{

    $tekpoints = tekpoint::all();
    $contributors = contributor::pluck('firstName');
    $communities = community::pluck('name','id');

// query and covert tekpoint wkt Geom to GeoJson $get = DB::table('tekpoints')->select('*');

// build feature objects

         foreach( $tekpoints as $tekpoint) {
                $properties = array( 'Common Name' => "$tekpoint->tradPlaceName",'Traditional Name' => "$tekpoint->tradPlaceName" , "Description" => "$tekpoint->description" , "First Hand" => "$tekpoint->firstHand","Second Hand" => "$tekpoint->secondHand", "Icon" => "$tekpoint->icon");
                $collection = $get->Select(DB::raw('ST_AsGeoJSON(geom) AS geometry'))->get();
               

                // try to pull geometry from multi-dimensional array
              
               $geometry = array_get($collection, 'items.geometry');
            

               $only = $collection->only(['$items.geometry']);
               $only->all();
               
             
                $feature = array(
                "type" => 'Feature',
                "properties" => $properties,
                "geometry" =>  $geometry 
                );
            }
    # Build GeoJSON feature collection array
            $geojson = array(
                    'type'      => 'FeatureCollection',
                    'features'  =>  $feature );

              
             $finalGeojson= json_encode($geojson);
                 dd($finalGeojson);
                

            return view('map', compact('finalGeojson','communities','contributors', 'tekpoints'));
            
          
              
                
           
            }

   

} '''

thanks for the read

Please sign in or create an account to participate in this conversation.