2

I am working on an application using Autodesk MapGuide. The application is being developed on PHP and hosted in IIS 7.5.

I had to create point, line and polygon geometries on-the-fly. I found a working code sample and customized it for my logic. The sample writes the geometry in an SDF (Autodesk Spatial Data File) file. The code writes only three fields - ID, Text and Geometry. The ID field is "AutoIncrement"

Now, I have to store the remaining data about the feature drawn on map (and stored in SDF) into a table in MySQL. For that, I have to fetch the ID of the last entry into the SDF file, which is not exposed at any point. Also, I have to select a row in MySQL table based on the feature I select on map/drawing (whose geometry is stored in SDF file) for updates. For that also, I have to again read/query the SDF.

How can I achieve that using PHP?

Thanks

4
  • Provide the code used to actually write the SDF. Any code that reads the SDF after you have written the file can pose concurrency problems at some point.
    – johanvdw
    Jan 20, 2012 at 18:15
  • @johanvdw: Sorry, but I have not written the code myself. I have used the code sample already provided by AutoDesk. See the link in my original question. It is a interaction of few classes. I don't think I can post the code here.
    – Kumar Kush
    Jan 20, 2012 at 19:02
  • Are you using autocad map? Not sure about your code but map can export the sdf "with" attibutes. it is also possible to create links to "join" the data you described.
    – Brad Nesom
    Jan 20, 2012 at 19:28
  • No. I am not using AutoCAD Map. The code is generating SDF on the fly.
    – Kumar Kush
    Jan 20, 2012 at 20:08

2 Answers 2

3

First of all: your original question is bad. You will may get inconsistensies if 2 users are using the file at the same time and your mysql server is suddenly responding slower or not at all, which may mean that properties are added to the wrong id.

It may therefore be better to add all data to mysql, including the spatial data. There is an FDO provider for mysql data, so you can add them to the map as well.

It is still possible to use a seperate sdf file, but in that case you should make sure that you don't read the file seperately from inserting the data. Luckily, this is possible: check where you use UpdateFeatures using an MgInsertFeatures object. In this case:

If the command is of type MgInsertFeatures, the property type is an MgPropertyType::Feature, and its value is a MgFeatureReader object. The feature reader object contains the set of properties inserted into the datastore by the insert command.

http://mapguide.osgeo.org/files/mapguide/docs/2.0/d5/d10/class_mg_feature_service.htm

You now discard the result of the UpdateFeatures function. Keep it and use it.

2
  • Thanks for reply. As for your point for inconsistencies, that won't be a problem because only the ID's fetched from the objects drawn on map will be used to populate records in MySQL. Talking about adding spatial data to MySQL, YES, it should be a good idea. I have manage to read and write spatial data into MySQL on my localhost Apache server. But the same code does not seem to work in MapGuide running on IIS, which you know, DOES NOT have all the facilities as in Apache Server. As per your description and the link you gave, using MgInsertFeatures should do the trick for me.
    – Kumar Kush
    Jan 21, 2012 at 6:17
  • I traced back the code for adding features to the SDF. It exactly follows the route as mentioned in the [MgInsertFeatures Class Reference]. mapguide.osgeo.org/files/mapguide/docs/2.0/d4/d63/…
    – Kumar Kush
    Jan 21, 2012 at 6:36
3

Here's the complete code for reading through SDF:

<?php
    session_start();
    require_once '<your path>/common.php';
    try{
        $args = ($_SERVER['REQUEST_METHOD'] == 'POST')? $_POST : $_GET;
        $sessionId = $args['SESSION'];
        $mapName = $args['MAPNAME'];

        MgInitializeWebTier ($webconfigFilePath);
        $userInfo = new MgUserInformation($sessionId);
        $siteConnection = new MgSiteConnection();
        $siteConnection->Open($userInfo);

        try{
            $resourceService = $siteConnection->CreateService(MgServiceType::ResourceService);
            $featureService = $siteConnection->CreateService(MgServiceType::FeatureService);

            $map = new MgMap();
            $map->Open($resourceService, $mapName);

            $selection = new MgSelection($map, $_POST['SELECTION']);
            $layers = $selection->GetLayers();
            if($layers->GetCount() > 0){
                $layer = $layers->GetItem(0);
                if($layer){
                    $layerClassName = $layer->GetFeatureClassName();
                    $selectionString = $selection->GenerateFilter($layer,$layerClassName);
                    $layerFeatureId = $layer->GetFeatureSourceId();

                    // Collect objects to be passed to SelectFeatures function
                    $layerFeatureResource = new MgResourceIdentifier($layerFeatureId);
                    $queryOptions = new MgFeatureQueryOptions();
                    $queryOptions->AddFeatureProperty('Field 1');
                    $queryOptions->AddFeatureProperty('Field 2');
                    $queryOptions->SetFilter($selectionString);
                    try{
                        // Create the reader
                        $featureReader = $featureService->SelectFeatures($layerFeatureResource,$layerClassName, $queryOptions); 
                        $featureReader->ReadNext();
                        $field1= $featureReader->GetString('Field 1');
                        $field2 = $featureReader->GetString('Field 2');
                        echo $field1 .' - '.$field2;
                    }
                    catch(MgApplicationException $ex){
                        echo '<b>Exception: </b>'.$ex->GetDetails();
                    }
                }
            }
        }
        catch (MgException $e){
            echo $e->GetExceptionMessage();
            echo $e->GetDetails();
        }
    }
    catch (MgException $e){
        echo $e->GetExceptionMessage();
        echo $e->GetDetails();
    }
?>

Note that the value $_POST['SELECTION'] was sent via a hidden field:

<input type="hidden" name="SELECTION" id="hidSelectionXML" />

And it was populated by this line in JavaScript, before the form was redirected to this code:

document.getElementById('hidSelectionXML').value = parent.parent.mapFrame.GetSelectionXML();

For this code to work, you should have selected feature (point, line, circle, etc.) on the map.

Hope this helps someone else's hours of frustration.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.