[mapguide-users] SDF import and export (MGOS 1.1)

djonio djonio at miami-airport.com
Fri Dec 21 12:11:05 EST 2007


I recently had a need to “import" some features into a session’s repository.
I searched the group for import/export and came across Jim O’Leary’s thread:
Export SDF

In addition and on a slightly different front, I finally got around to test
and see if I could update the session repository of a client(A) via another
client(B) on a different machine. Of course this works too … and does make
it possible to do a “kind’a sort’a” serverside push. If only there was a
good way to pass around that MapGuide sessionID!

Since I live in a .NET world:

IMPORTING:
   public void WriteTrakTraceFromFile(string session, string mapname, string
fileName, bool appendCurrent)
    {
        if (this._site == null)
        {
            this.SetUp(session, mapname);
        }
        byte[] rtrnBytes = null;
        MgResourceService resourceService =
(MgResourceService)_site.CreateService(MgServiceType.ResourceService);
        MgMap map = new MgMap();
        map.Open(resourceService, mapname);
        // This is the actual FeatureSource for the TrakTrace layer
        StringBuilder sb = new StringBuilder();
        sb.Append(_Session_StringBuilder.ToString());
        sb.Append(_layerName);
        sb.Append(".");
        sb.Append(MgResourceType.FeatureSource);
        MgResourceIdentifier markupSdfResId = new
MgResourceIdentifier(sb.ToString());
        try
        {
            // We go get the disk file contents - this MgByteReader is very,
very flacky ... 
            // So we just get the bytes out of the crippled-up thingee and
be done with it.
            MgByteReader byteReader = new MgByteReader(fileName,
MgMimeType.Binary, false);
            using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
            {
                byte[] buf = new byte[1024];
                int c = 0;
                do
                {
                    c = byteReader.Read(buf, buf.Length);
                    ms.Write(buf, 0, c);
                } while (c != 0);
                ms.Position = 0;
                rtrnBytes = ms.ToArray();
            }
            // Set-up to copy the CURRENT over to a temporary resource. All
within the session context.
            // ... just change the name a little
            sb.Length = 0;
            sb.Append(_Session_StringBuilder.ToString());
            sb.Append(_layerName + "_tmp");
            sb.Append(".");
            sb.Append(MgResourceType.FeatureSource);
            MgResourceIdentifier markupSdfResId_tmp = new
MgResourceIdentifier(sb.ToString());
            // copy the CURRENT over to _tmp named resource
            resourceService.CopyResource(markupSdfResId, markupSdfResId_tmp,
true);
            // It took me some time to noodle out the name that was used in
the SESSION repository
            // I actually opened up the REPOSITORY objects with a hex editor
and scanned though them.
            // DUH ... maybe next time I will apply some common sense. SDF
file .sdf extention ...
            // who would have thought that!!!
            // Now we must delete the CURRENT resource. SetResourceData()
does not seem to like any other way. 
            resourceService.DeleteResourceData(markupSdfResId, _layerName +
".sdf");
            // finally we bring in the byte[] to the CURRENT
            resourceService.SetResourceData(markupSdfResId, _layerName +
".sdf", "File", new MgByteReader(rtrnBytes, rtrnBytes.Length,
MgMimeType.Binary));
            // If we wish to append and not just do a replace we do that
here 
            int appended_rows = 0;
            if (appendCurrent == true)
                appended_rows = AppendTrakTraceRows(session, mapname,
markupSdfResId_tmp, markupSdfResId);
            // ... of course ... save it           
            map.Save(resourceService);
        }
        catch (System.Exception)   {  }
    }

APPENDING:
    private int AppendTrakTraceRows(string session, string mapname,
MgResourceIdentifier fromResID, MgResourceIdentifier toResID)
    {
        if (this._site == null)
        {
            this.SetUp(session, mapname);
        }
        // One would think that there is a really neat way to do this using
a SQL statement(s)
        // but since I have not a clue ... we do what we know how to do ....
        int rtn_appended_count = 0;
        MgResourceService resourceService =
(MgResourceService)_site.CreateService(MgServiceType.ResourceService);
        MgFeatureService featureService =
_site.CreateService(MgServiceType.FeatureService) as MgFeatureService;
        MgMap map = new MgMap();
        map.Open(resourceService, mapname);
        MgFeatureQueryOptions queryOptions = new MgFeatureQueryOptions();
        MgPropertyCollection propertyValues = new MgPropertyCollection();
        MgFeatureCommandCollection commands = new
MgFeatureCommandCollection();
        // Open up the FROM location, read all the rows and populate the TO
        MgFeatureReader featureReader =
featureService.SelectFeatures(fromResID, _className, queryOptions);        
        while (featureReader.ReadNext())
        {
            // The identity column is autogenerated 
            propertyValues.Add(new MgStringProperty(_TEXTpropertyName,
featureReader.GetString(_TEXTpropertyName)));
            propertyValues.Add(new MgDateTimeProperty(_DTpropertyName,
featureReader.GetDateTime(_DTpropertyName)));
            propertyValues.Add(new MgGeometryProperty(_GEOMETRYpropertyName,
featureReader.GetGeometry(_GEOMETRYpropertyName)));
            // construct the insert command
            commands.Add(new MgInsertFeatures(_className, propertyValues));
            // actually insert the "row" into the TO
            featureService.UpdateFeatures(toResID, commands, false);
            // remove all the properties
            propertyValues.Clear();
            // remove the last insert command
            commands.Clear();
            rtn_appended_count = rtn_appended_count + 1;
        }
        return rtn_appended_count;
    }

EXPORTING:
    [WebMethod]
    public string TrakTraceToFile(string session, string mapname)
    {
        if (this._site == null)
        {
            this.SetUp(session, mapname);
        }
        string rtn_val = null;
        string session_repository_file_name = _layerName + ".sdf";
        // Should really have a nice place to put these files ... server
deserves better treatment
        string tmptmpfileName = Path.GetTempFileName();
        try
        {
            System.IO.File.Delete(tmptmpfileName); // At least I can clean
things up a little
        }
        catch (System.Exception) { }
        string tmpfileName = tmptmpfileName + ".sdf";
       
        MgResourceService resourceService =
(MgResourceService)_site.CreateService(MgServiceType.ResourceService);
        MgMap map = new MgMap();
        map.Open(resourceService, mapname);
        MgResourceIdentifier markupSdfResId = null;
        StringBuilder sb = new StringBuilder();
        sb.Append(_Session_StringBuilder.ToString());
        sb.Append(_layerName);
        sb.Append(".");
        sb.Append(MgResourceType.FeatureSource);
        try
        {
            markupSdfResId = new MgResourceIdentifier(sb.ToString());
            // WOW!!!
            // Now in this case the MgByteReader works and the MgByteSink
does not ..?
            MgByteReader byteReader =
resourceService.GetResourceData(markupSdfResId,
session_repository_file_name);          
            byteReader.ToFile(tmpfileName);
            rtn_val = tmpfileName;
        }
        catch (ManagedException) {  }
       
        return rtn_val;
    }

-- 
View this message in context: http://www.nabble.com/SDF-import-and-export-%28MGOS-1.1%29-tp14458845s16610p14458845.html
Sent from the MapGuide Users mailing list archive at Nabble.com.



More information about the mapguide-users mailing list