FMEOWriter allows you to create a new dataset in a supported format. The methods and attributes are as follows:
This chapter will learn:
- Create writer
- Open writer
- Obtain user writer Parameters
- Obtain information from the active writer
- Creation element
- Close writer
For the relationship between wirter and coordinate system, see Chapter 7th of this series of tutorials, using coordinate system.
Create writer
You must use the createWriter method of FMESession to create a writer object. The Code is as follows:
Dim fmeDirectives As FMEOStringArray
Set fmeDirectives = m_fmeSession.createStringArray
FmeDirectives. append ("OUTPUT_STATS ")
FmeDirectives. append ("NO ")
Set m_fmeWriter = m_fmeSession.createWriter ("SHAPE ",_
FmeDirectives)
The first parameter of createWriter is in the specified format, in upper case: SHAPE, which is used to specify ESRI Shapefile Writer. For details about setting this parameter, see the FME Reader and Writers manual.
Note: whether a format is writable depends on the Reader/Writer items in the Manual table.
The second parameter is a string array that contains the writer creation parameters. For specific settings, see the following table:
Note: The preceding parameters must be passed to the createWriter method using a string array.
Open Writer
Once a writer is created, you can use it to open a dataset. The following Code creates a SHAPE Writer and opens the c: \ shape dataset.
Set fmeKeywords = m_fmeSession.createStringArray
FmeKeywords. append ("MEASURES_AS_Z ")
FmeKeywords. append ("YES ")
Call m_fmeWriter.open ("c: \ shape", fmeKeywords)
The first parameter of the open method specifies the dataset. For Shapefile writer, this parameter is a directory. For other Writer, a file, database, or URL may be required.
The second parameter is a string array, which contains the writer control parameters and is formed using the name and value pairs. For the parameter list, see the FME Readers and Writers manual.
Note: All writer supports the DEF keyword. The FME object treats the DEF keyword differently from other keywords.
Obtain user input parameters
You can use the destPrompt method of FMEODialog to obtain parameters interactively. This dialog box allows you to specify the format, dataset, and other parameters.
For example:
Note: This dialog box is only available on WINDOWS.
This dialog box returns parameters that can be used to generate the createWriter method of FMEOSession and the open method of FMEOWWriter. For example:
Public Sub DestPrompt (sDestDir As String)
Dim fmeDialog As FMEODialog
Dim fmeDirectives As FMEOStringArray
Dim fmeKeywords As FMEOStringArray
Dim sDataset As String
Dim sFormat As String
Dim bCompleted As Boolean
Set fmeDialog = m_fmeSession.createDialog
Set fmeDirectives = m_fmeSession.createStringArray
Call fmeDirectives. append ("LIMIT_FORMATS ")
Call fmeDirectives. append ("MIF ")
BCompleted = fmeDialog. DestPrompt ("MIF", sDestDir ,_
SFormat, sDataset, fmeDirectives)
If bCompleted = True Then
Set m_fmeWriter = m_fmeSession.createWriter (_
SFormat ,_
FmeDirectives)
Set fmeKeywords = m_fmeSession.createStringArray
Call m_fmeWriter.open (sDataset, fmeKeywords)
End If
End Sub
The following parameters are supported by the destPrompt method:
Obtain information from the active Writer
If your application needs to access the list of available formats, you can use the getAvailableFormats method of the FMEODialog object. For details about any format, you can use the getFormatInfoEx method, for more information, see section 4 of this series.
Write Element
The application needs to create new elements and save them to the target dataset. For example, you need to annotate the map and save these annotations.
The write element requires two steps. First, use the addSchema method to add a schema for each element. If the Schema Required item is YES in the parameter table of the manual, the Schema is Required. If NO, writer ignores the element schema. Once all schemas are specified, the second step can use the write method to write data elements.
Use the createFeature method of FMEOSession to create an empty schema element and specify the following information:
- Element type: the element type is set through the featureType attribute. The element type depends on the output format, such as theme, layer, level, file, and database table name. For the element type, see the reference table of the manual.
- User attribute list: if the format supports user attributes, the user attributes can be added to the sequencedAddtribute attribute.
- FME type list: Use the setlistattriry method to set the fme_geometry list attribute with a valid fme_type value.
For example:
Dim fmeSchemaFeature As FMEOFeature
Dim fmeGeomList As FMEOStringArray
Set fmeSchemaFeature = m_fmeSession.createFeature
FmeSchemaFeature. featureType = "tracts"
FmeSchemaFeature. sequencedAttribute ("CNTY_FIPS") = _
"Fme_decimal (15,3 )"
FmeSchemaFeature. sequencedattrieter ("PERIMETER") = _
"Fme_decimal (15,3 )"
FmeSchemaFeature. sequencedAttribute ("SQ_MILES") = _
"Fme_decimal (15,3 )"
FmeSchemaFeature. sequencedattriature ("STATE") = _
"Fme_char (20 )"
Set fmeGeomList = m_fmeSession.createStringArray
FmeGeomList. append ("fme_area ")
Call fmeSchemaFeature. setlistattriry ("fme_geometry ",_
FmeGeomList)
Note: you do not need to specify the Geometry attribute for the schema element. This attribute is implicitly specified.
A schema element is created and passed to writer. An application can write schema-compliant data elements to a dataset. For example:
Call m_fmeWriter.addSchema (fmeSchemaFeature)
LFeatureCount = m_fmeFeatureVector.entries
For I = 0 To lFeatureCount-1
Set fmeFeature = m_fmeFeatureVector.element (I)
Call m_fmeWriter.write (fmeFeature)
Next I
The code above assumes that all elements are stored in fmeDataFeatureVector and have the same fmeSchemaFeaure.
Close Writer
After the last element is written to the dataset, you must close the writer. If you do not close the writer, the dataset may become invalid. You can close it using the close method:
Call m_fmeWriter.Close
After closing, you can use the VB statement to release related resources:
Set m_fmeWriter = Nothing
References:
Building Applications with FME Objects February 2005
Reprinted please indicate the source http://www.cnblogs.com/booolee